C# 分割函数问题

发布于 2024-12-03 00:12:11 字数 865 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

柳若烟 2024-12-10 00:12:11

试试这个:

string[] arr = value.Replace("," , " ").Split(' ');

Try this:

string[] arr = value.Replace("," , " ").Split(' ');
筱武穆 2024-12-10 00:12:11

不带参数的 Split() 默认情况下按空格字符分割字符串。如果你想用其他字符分割字符串,你应该使用接受分隔符列表的重载:

string[] splitvalues = value.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:

string[] splitvalues = value.Split(' ', ',');
烟花易冷人易散 2024-12-10 00:12:11

不带任何参数的 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 ?

夜空下最亮的亮点 2024-12-10 00:12:11

您在空格上进行了分割,但字符串中也有一些逗号:

"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.

将军与妓 2024-12-10 00:12:11

这看起来你的数据应该被分割两次,“,”似乎是你的记录分隔符,“”似乎是你的字段分隔符;

要使用此数据,您可能需要使用拆分行,然后在行上使用正则表达式

Regex fieldRegex=new Regex("(?<key>.*?)\s(?<value>.*?)");
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[] lines= value.Split(',');
foreach (var line in lines) {
  matches=fieldRegex.Matches(line);
  foreach (var match in matches.OfType(Match)) {
     string key=match.Groups["key"].Value;
     string value=match.Groups["value"].Value;
     switch (key) {
       case "L3":  
          Name = value;
          break;  
       case "L4":  
          Number1.FromString(value);  
          break;  
      case "L5":  
          Number2.FromString(value);  
          break;  
      case "L2":  
          Number3.FromString(value);  
          break;  
     }  
  }
}

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

Regex fieldRegex=new Regex("(?<key>.*?)\s(?<value>.*?)");
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[] lines= value.Split(',');
foreach (var line in lines) {
  matches=fieldRegex.Matches(line);
  foreach (var match in matches.OfType(Match)) {
     string key=match.Groups["key"].Value;
     string value=match.Groups["value"].Value;
     switch (key) {
       case "L3":  
          Name = value;
          break;  
       case "L4":  
          Number1.FromString(value);  
          break;  
      case "L5":  
          Number2.FromString(value);  
          break;  
      case "L2":  
          Number3.FromString(value);  
          break;  
     }  
  }
}
夜未央樱花落 2024-12-10 00:12:11

您可以使用正则表达式 L(\d+)\s([^,\s]+),例如:

var result =    
    Regex.Matches(input, @"L(\d+)\s([^,\s]+)").OfType<Match>()
    .ToDictionary(k => int.Parse(k.Groups[1].Value), v => v.Groups[2].Value);

You can use regex L(\d+)\s([^,\s]+), e.g.:

var result =    
    Regex.Matches(input, @"L(\d+)\s([^,\s]+)").OfType<Match>()
    .ToDictionary(k => int.Parse(k.Groups[1].Value), v => v.Groups[2].Value);
墨洒年华 2024-12-10 00:12:11

尝试这样

在代码

using System.Collections.Generic;;

程序中添加此内容

        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(',');
        List<string> outP = new List<string>();
        foreach (string s in splitvalues)
        {
            string[] innerSplit = s.Split();
            //use this 'innerSplit' variable for furture coding...
            outP.AddRange(innerSplit);
        }

Try like this

Add this in code

using System.Collections.Generic;;

program

        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(',');
        List<string> outP = new List<string>();
        foreach (string s in splitvalues)
        {
            string[] innerSplit = s.Split();
            //use this 'innerSplit' variable for furture coding...
            outP.AddRange(innerSplit);
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文