开发者

C# Split function problem

开发者 https://www.devze.com 2023-04-02 18:12 出处:网络
string value = \"L3 ABCD L4 3501% L5 20%,L3 EFGH L4 1开发者_如何学编程7260% L5 20%,L3 IJKL L4 2051% L5 20%,L3 MNOP L4 2621% L5 20%,L3 QRST L4 45325% L5 20% L2 40%\";
string value = "L3 ABCD L4 3501% L5 20%,L3 EFGH L4 1开发者_如何学编程7260% 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;
    }
}


Try this:

string[] arr = value.Replace("," , " ").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(' ', ',');


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 ?


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

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;  
     }  
  }
}


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);


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);
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消