Remove text after a string occurrence

后端 未结 5 1098
温柔的废话
温柔的废话 2021-01-19 05:38

I have a string that has the following format:

string sample = \"A, ABC, 1, ACS,,\"

As you can see, there are 5 occurences of the ,

5条回答
  •  醉酒成梦
    2021-01-19 06:15

    You could do something like this:

    sample.Split(',').Take(4).Aggregate((s1, s2) => s1 + "," + s2).Substring(1);
    

    This will split your string at the comma and then take only the first four parts ("A", " ABC", " 1", " ACS"), concat them to one string with Aggregate (result: ",A, ABC, 1, ACS") and return everything except the first character. Result: "A, ABC, 1, ACS".

提交回复
热议问题