I'm using Regex.Split()
to take the user input and turn it into individual words in a list but at the moment it removes any spaces they add, I would like it to keep the whitespace.
string[] newInput = Regex.Split(updatedLine, @"\s+");
string text = "This is some text";
var splits = Regex.Split(text, @"(?=(?<=[^\s])\s+)");
foreach (string item in splits)
Console.Write(item);
Console.WriteLine(splits.Count());
This will give you 4 splits each having all the leading spaces preserved.
(?=\s+)
Means split from the point where there are spaces ahead. But if you use this alone it will create 15 splits on the sample text because every space is followed by another space in case of repeated spaces.
(?=(?<=[^\s])\s+)
This means split from a point which has non space character before it and it has spaces ahead of it.
If the text starts from a space and you want that to be captured in first split with no text then you can modify the expression to following
(?=(?<=^|[^\s])\s+)
Which means series of spaces need to have a non space character before it OR start of the string.
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