我正在尝试将string
拆分为List<string>
。我有这个字符串:
string myData = "one, two, three; four, five, six; seven, eight, nine";
我希望填充的字符串列表看起来像:
one two three
four five six
seven eight nine
这意味着我必须删除逗号(,
)和半冒号(;
),以便例如列表的第一行,第二列将是两个(没有逗号) ,半冒号或空格)。
我知道我可以使用.Split
:
string[] splittedArray = myData.Split(';').ToArray();
这应该产生如下结果:
one, two, three,
four, five, six,
seven, eight, nine
如何删除逗号(,
)并将其以该格式放入列表中?
答案 0 :(得分:13)
myData.Replace(",", String.Empty).Split(';').ToList();
答案 1 :(得分:4)
试试这个
string myData = "one, two, three; four, five, six; seven, eight, nine";
string[] splittedArray = myData.Replace(",", "").Split(';').ToArray();
List<string> list = splittedArray.ToList();
答案 2 :(得分:2)
string[] splittedArray = myData.Split(';')
.Select(x => x.Replace(",","")
.ToArray();
或者:
string[] splittedArray = myData.Split(';')
.Select(x => string.Join(" ", x.Split(','))
.ToArray();
答案 3 :(得分:2)
试试这个:
string myData = "one, two, three; four, five, six; seven, eight, nine";
List<string> list = myString.Replace(", ", " ").Split(';').ToList();
答案 4 :(得分:2)
再使用一个Split
var splittedArray = myData.Split(';').Select(s => s.Split(',').ToArray()).ToArray();
所以splittedArray[0][1]
将为two