SoFunction
Updated on 2025-04-06

Replace multiple commas in a string of character with one standard separator (regular expression)


/// <summary>
/// Format a set of strings such as ",,a,s,,d,c" to a standard delimited string such as "a,s,d,c";
/// </summary>
/// <param name="str">ref Characters that need to be formatted</param>
private void strFormat(ref string str)
{
string regexText = "\\,{2,}";
string[] strArray = (str,regexText);
str = "";
foreach (string strf in strArray)
{
if(strf != "")
{
str += strf + ",";
}
}
//The above code obtains xx,xxx,x,xx,x,x,x, then the last comma needs to be removed and meets the standard separator conditions.
str = (0, - 1);
}