if anyone can help me with this one...
I have a string named text. I need to ensure that after every occurrence of a specific character " there is a whitespace. If there is not a whitespace, then I need to insert one.
I am not sure on the best approach to accomplish this in c#, I think regular expressions may be the way to go, but I do not have enough knowledge about regular expressions to do this...
If anyone can help it would be appreciated.
// rule: all 'a's must be followed by space.
// 'a's that are already followed by space must
// remain the same.
String text = "banana is a fruit";
text = Regex.Replace(text, @"a(?!\s)", x=>x + " ");
// at this point, text contains: ba na na is a fruit
The regular expression a(?!\s) searches for 'a' that is not followed by a whitespace. The lambda expression x=>x + " " tells the replace function to replace any occurence of 'a' without a following whitespace for 'a' with a whitespace
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With