Regex removing double/triple comma in string

前端 未结 5 851
一生所求
一生所求 2021-02-15 00:39

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:08

    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  
    

提交回复
热议问题