I am building a string of last names separated by hyphens. Sometimes a whitespace gets caught in there. I need to remove all whitespace from end result.
Sample string to work on:
Anderson -Reed-Smith
It needs to end up as (no space after Anderson):
Anderson-Reed-Smith
The last name string is in a string variable, LastName.
I am using a regular expression:
Regex.Replace(LastName, @"[\s+]", "");
The result of this is:
Anderson -Reed-Smith.
I also tried:
Regex.Replace(LastName, @"\s+", "");
and
Regex.Replace(LastName, @"\s", "");
What am I doing wrong?
Best Answer
Instead of a
RegEx
useReplace
for something that simple: