1

エラーについて申し訳ありませんが、質問を更新しています。次の形式で入力を受け取るアプリケーションを作成しています。

someId=00000-000-0000-000000;someotherId=123456789;someIdentifier=3030;

これらの値をジェネリックLIST<T>に追加して、リストに次のものが含まれるようにする方法はありますか

record.someid= 00000-000-0000-000000
record.someotherId =123456789
record.someIdentifier =   3030

初心者なのでこんな質問で申し訳ありません。

4

5 に答える 5

7
var input = "someId=00000-000-0000-000000;someotherId=123456789;someIdentifier=3030;"
var list = input.Split(';').ToList();

ファイルのヘッダーに追加した後:

using System.Linq;
于 2013-01-02T16:00:03.323 に答える
3

を使用Splitして、組み合わせと思われる文字列の一部を取得しkey / value pair、キーと値のペアを に追加できますDictionary

 string str = "someId=00000-000-0000-000000;someotherId=123456789;someIdentifier=3030";
 string [] arr = str.Split(';');
 Dictionary<string, string> dic = new Dictionary<string, string>();
 for(int i=0; i < arr.Length; i++)
 {
        string []arrItem = arr[i].Split('=');
        dic.Add(arrItem[0], arrItem[1]);            
 }

OP のコメントに基づいて編集し、カスタム クラス リストに追加します。

internal class InputMessage
{
     public string RecordID { get; set;}
     public string Data { get; set;}
}

 string str = "someId=00000-000-0000-000000;someotherId=123456789;someIdentifier=3030";
    string [] arr = str.Split(';');
List<InputMessage> inputMessages = new List<InputMessage>();
for(int i=0; i < arr.Length; i++)
{
       string []arrItem = arr[i].Split('=');
    inputMessages.Add(new InputMessage{ RecordID = arrItem[0], Data = arrItem[1]});         
}
于 2013-01-02T16:07:54.370 に答える
2

フォーマットが常に厳密である場合は、 を使用できますstring.Split。を作成できますLookup

string str = "someId=00000-000-0000-000000;someotherId=123456789;someIdentifier=3030;";
var idLookup = str.Split(new[]{';'}, StringSplitOptions.RemoveEmptyEntries)
    .Select(token => new { 
        keyvalues=token.Split(new[]{'='}, StringSplitOptions.RemoveEmptyEntries)
    })
    .ToLookup(x => x.keyvalues.First(), x => x.keyvalues.Last());

// now you can lookup a key to get it's value similar to a Dictionary but with duplicates allowed
string someotherId = idLookup["someotherId"].First();

デモ

于 2013-01-02T16:16:33.913 に答える
1

Tこの場合、あなたはそれがどうなるかを知る必要がありますList<T>、私はそれを文字列として置きます。よくわからない場合は使用してくださいobject

List<object> objList = str.Split(new char[] { ';' }).ToList<object>();
于 2013-01-02T16:01:21.103 に答える
1

次のコードから使用できます。

        string str = "someId=00000-000-0000-000000;someotherId=123456789;someIdentifier=3030;";

        int Start, End = 0;

        List<string> list = new List<string>();

        while (End < (str.Length - 1))
        {
            Start = str.IndexOf('=', End) + 1;
            End = str.IndexOf(';', Start);

            list.Add(str.Substring(Start, End - Start));
        } 
于 2013-01-02T18:44:49.957 に答える