C# 分割函数问题
string value = "L3 ABCD L4 3501% L5 20%,L3 EFGH L4 17260% L5 20%,L3 IJKL L4 2051% L5 20%,L3 MNOP L4 2621% L5 20%,L3 QRST L4 45325% L5 20% L2 40%";
然后我尝试
string[] splitvalues = value.Split();
使用开关来查明它是否是 L2、L3、L4、L5
但 L5 处的值正在获取“20%,L3”
代码是:
string[] splitvalues = value.Split();
for (int i = 0; i < splitvalues .Length; i = i + 2)
{
String id = splitvalues [i];
switch (id)
{
case "L3":
Name = splitvalues [i + 1];
break;
case "L4":
Number1.FromString(splitvalues [i + 1]);
break;
case "L5":
Number2.FromString(splitvalues [i + 1]);
break;
case "L2":
Number3.FromString(splitvalues [i + 1]);
break;
}
}
string value = "L3 ABCD L4 3501% L5 20%,L3 EFGH L4 17260% L5 20%,L3 IJKL L4 2051% L5 20%,L3 MNOP L4 2621% L5 20%,L3 QRST L4 45325% L5 20% L2 40%";
I am trying
string[] splitvalues = value.Split();
then using a switch to find out whether it is L2, L3, L4, L5
But value at L5 is fetching "20%,L3"
The code is:
string[] splitvalues = value.Split();
for (int i = 0; i < splitvalues .Length; i = i + 2)
{
String id = splitvalues [i];
switch (id)
{
case "L3":
Name = splitvalues [i + 1];
break;
case "L4":
Number1.FromString(splitvalues [i + 1]);
break;
case "L5":
Number2.FromString(splitvalues [i + 1]);
break;
case "L2":
Number3.FromString(splitvalues [i + 1]);
break;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
试试这个:
Try this:
不带参数的 Split() 默认情况下按空格字符分割字符串。如果你想用其他字符分割字符串,你应该使用接受分隔符列表的重载:
Split() without parameters splits string by space chars by default. If you want to split the string by other chars you should use overload that accepts list of separator characters:
不带任何参数的 Split() 仅在空格上拆分,因此 splitValues[5] 作为 '20%L3' 是正确的。
您能否澄清您的问题以描述您想要实现的目标?
Split() without any parameters only splits on Spaces, so splitValues[5] as '20%L3' is correct.
Can you clarify your question to describe what you want to achieve ?
您在空格上进行了分割,但字符串中也有一些逗号:
"L3 ABCD L4 3501% L5 20%,L3 EFGH L4 17260% L5 20%,L3 IJKL L4 2051% L5 20%,L3 MNOP L4 2621% L5 20%,L3 QRST L4 45325% L5 20% L2 40%"
删除逗号,就可以开始了。
如果无法修改字符串,请按空格和逗号分隔。
You're splitting on space, but you also have some commas in the string:
"L3 ABCD L4 3501% L5 20%,L3 EFGH L4 17260% L5 20%,L3 IJKL L4 2051% L5 20%,L3 MNOP L4 2621% L5 20%,L3 QRST L4 45325% L5 20% L2 40%"
Remove the commas and you should be good to go.
If you can't modify the string, then split on whitespace and commas.
这看起来你的数据应该被分割两次,“,”似乎是你的记录分隔符,“”似乎是你的字段分隔符;
要使用此数据,您可能需要使用拆分行,然后在行上使用正则表达式
This looks like your data is supposed to be split twice, ',' appears to be your record seperator and ' ' appears to be your field seperator;
To use this data you probably want to use split the rows and then use a regex on the lines
您可以使用正则表达式
L(\d+)\s([^,\s]+)
,例如:You can use regex
L(\d+)\s([^,\s]+)
, e.g.:尝试这样
在代码
程序中添加此内容
Try like this
Add this in code
program