Regex removing double/triple comma in string

前端 未结 5 1815
天命终不由人
天命终不由人 2021-02-15 01:00

I need to parse a string so the result should output like that:

\"abc,def,ghi,klm,nop\"

But the string I am receiving could looks more like tha

5条回答
  •  花落未央
    2021-02-15 01:18

    Here is my effort:

    //Below is the test string 
    string test = "YK     002       10        23           30         5       TDP_XYZ  "
    private static string return_with_comma(string line)
        {
            line = line.TrimEnd();
            line = line.Replace("  ", ",");
            line = Regex.Replace(line, ",,+", ",");
            string[] array;
            array = line.Split(',');
            for (int x = 0; x < array.Length; x++)
            {
                line += array[x].Trim();
            }
            line += "\r\n";
            return line;
        }
     string result = return_with_comma(test);
     //Output is
     //YK,002,10,23,30,5,TDP_XYZ  
    

提交回复
热议问题