I have a string "a ; b; c d; e"
How can I remove the white space around ";" but keep the one between chars. So after replacement, I want to get "a;b;c d;e"
Thanks
I have a string "a ; b; c d; e"
How can I remove the white space around ";" but keep the one between chars. So after replacement, I want to get "a;b;c d;e"
Thanks
This would work if you only had one space before or after the ;
var clean = "a ; b; c d; e".Replace(" ;", ";").Replace("; ", ";");
If there could be multiple spaces before or after the ;, you could run it in a loop that's exit condition was when neither " ;" or "; " was found
Alternatively, a regex would work perfectly for this.
Use a regular expression.
I've added code
function stripSpacesKeepSemicolons(string dirty) {
private static Regex keepSemicolonStripSpacesRegex = new Regex("\\s*(;)\\s*");
return keepSemicolonStripSpacesRegex.Replace(dirty,"$1");
}