I have the following line to split a sentence into words and store it into an array based on white spaces: string[] s = Regex.Split(input, @"\s+");
The problem is at the end of the sentence, it also picks up the period. For example: C# is cool.
The code would store:
C#
is
cool.
The question is: How do I get it not to pick up the period ?
You can use a character class []
to add in the dot .
or other characters that you need to split on.
string[] s = Regex.Split(input, @"[\s.]+");
See Demo
You can add dot (and other punctuation marks as needed) to the regular expression, like this:
string[] s = Regex.Split(input, @"(\s|[.;,])+");
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