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
Actually, you can do it without any Trim calls.
text = Regex.Replace(text, "^,+|,+$|(?<=,),+", "");
should do the trick.
The idea behind the regex is to only match that, which we want to remove. The first part matches any string of consecutive commas at the start of the input string, the second matches any consecutive string of commas at the end, while the last matches any consecutive string of commas that follows a comma.
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
a simple solution without regular expressions :
string items = inputString.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
string result = String.Join(",", items);
You can use the ,{2,}
expression to match any occurrences of 2 or more commas, and then replace them with a single comma.
You'll probably need a Trim
call in there too, to remove any leading or trailing commas left over from the Regex.Replace
call. (It's possible that there's some way to do this with just a regex replace, but nothing springs immediately to mind.)
string goodString = Regex.Replace(badString, ",{2,}", ",").Trim(',');
Search for ,,+
and replace all with ,
.
So in C# that could look like
resultString = Regex.Replace(subjectString, ",,+", ",");
,,+
means "match all occurrences of two commas or more", so single commas won't be touched. This can also be written as ,{2,}
.