在foreach中单独的逗号和行

时间:2016-01-14 20:28:16

标签: c# string

如果可能,我想在foreach中分隔逗号和行。 20是npc的id,10是npc的量,逗号表示该npc。感谢您的帮助:))

string npcs = " 20 - 10 , 30 - 40 "; 
//or
string[] npcs = {" 20 - 10 ", " 30 - 40 "}; 

2 个答案:

答案 0 :(得分:2)

不确定我是否正确理解了这个问题,但我相信你想从一个字符串中检索你的NPC信息。如果是这样,这是我的解决方案:

string npcs = " 20 - 10 , 30 - 40 ";

foreach (var npc in npcs.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
    var npcInfo = npc.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
    string npcID = npcInfo[0];
    string npcAmount = npcInfo[1];
}

答案 1 :(得分:1)

您可以像这样使用Split

var _ = require('underscore'); // Javascript Library

Parse.Cloud.define("getNewPicture", function(request, response) {

    var username = request.params.username;
    var notWantedPics = [];

    if (!username) {
        return response.error('No username.');
    }

    var query1 = new Parse.Query("SeenPictures");
    query1.ascending("createdAt");
    query1.equalTo("username", username);
    var SeenPictures = query1.find();

    return Parse.Promise.when([SeenPictures]).then(function (SeenPictures) {

        SeenPictures = _.filter(SeenPictures, function (SeenPicture) {
            if (SeenPicture.get("likes") == 1 || SeenPicture.get("dislikes") == 1) {
                notWantedPics.push(SeenPicture.get("pictureId"));
                return false;
            } 
            else {
                return true;
            }
        });

        // notWantedPics?

        if (SeenPictures > 0) {
            var query2 = new Parse.Query("Pictures");
            var Pictures = [query2.get(SeenPictures[0].get('pictureId'))];
        } 
        else {
            var query2 = new Parse.Query("Pictures");
            query2.notEqualTo("photographerUserName", username);
            query2.notContainedIn("objectId", notWantedPics);
            query2.ascending("createdAt");
            var Pictures = query2.find();
        }

        return Parse.Promise.when([Pictures]);

    }).then(function (Pictures) {

        if (Pictures > 0) {

            // Success
            return response.success(Pictures[0]);

        } else {
            return Parse.Promise.error("No pictures.");
        }

    }, function (error) {
        // Error
        return response.error(error);
    });

});