I am trying to split a string
into a List<string>
. I have this string:
我试图将一个字符串拆分为List
string myData = "one, two, three; four, five, six; seven, eight, nine";
And I would like the filled list of strings to look like:
我希望填充的字符串列表看起来像:
one two three
four five six
seven eight nine
Meaning that I have to remove the commas(,
) and the semi colons(;
), so that for example the first row of the list, the second column will be two(without commas, semi colons or spaces).
这意味着我必须删除逗号(,)和半冒号(;),以便例如列表的第一行,第二列将是两个(没有逗号,半冒号或空格)。
I know that I can use .Split
:
我知道我可以使用.Split:
string[] splittedArray = myData.Split(';').ToArray();
This should produce a result like:
这应该产生如下结果:
one, two, three,
four, five, six,
seven, eight, nine
How do I remove the commas(,
) and put it in the list in that format?
如何删除逗号(,)并将其以该格式放入列表中?
5 个解决方案
#1
13
myData.Replace(",", String.Empty).Split(';').ToList();
#2
4
Try this
尝试这个
string myData = "one, two, three; four, five, six; seven, eight, nine";
string[] splittedArray = myData.Replace(",", "").Split(';').ToArray();
List<string> list = splittedArray.ToList();
#3
2
string[] splittedArray = myData.Split(';')
.Select(x => x.Replace(",","")
.ToArray();
Or:
要么:
string[] splittedArray = myData.Split(';')
.Select(x => string.Join(" ", x.Split(','))
.ToArray();
#4
2
Use one more Split
再使用一个Split
var splittedArray = myData.Split(';').Select(s => s.Split(',').ToArray()).ToArray();
So splittedArray[0][1]
will be two
所以splittedArray [0] [1]将是两个
#5
2
Try This:
尝试这个:
string myData = "one, two, three; four, five, six; seven, eight, nine";
List<string> list = myString.Replace(", ", " ").Split(';').ToList();
#1
13
myData.Replace(",", String.Empty).Split(';').ToList();
#2
4
Try this
尝试这个
string myData = "one, two, three; four, five, six; seven, eight, nine";
string[] splittedArray = myData.Replace(",", "").Split(';').ToArray();
List<string> list = splittedArray.ToList();
#3
2
string[] splittedArray = myData.Split(';')
.Select(x => x.Replace(",","")
.ToArray();
Or:
要么:
string[] splittedArray = myData.Split(';')
.Select(x => string.Join(" ", x.Split(','))
.ToArray();
#4
2
Use one more Split
再使用一个Split
var splittedArray = myData.Split(';').Select(s => s.Split(',').ToArray()).ToArray();
So splittedArray[0][1]
will be two
所以splittedArray [0] [1]将是两个
#5
2
Try This:
尝试这个:
string myData = "one, two, three; four, five, six; seven, eight, nine";
List<string> list = myString.Replace(", ", " ").Split(';').ToList();