I have a case where I need to split a string by spaces. Mostly, I have 2 spaces, but in certain instances when I have a negative (-) number, there is only 1 space.
Fresno St. Wyoming 3.00 1.12 2.83 2.51 3.69 -3.85 -8.88 1.20 1.00 -2.60 -2.64 6.90 -0.30 1.00 6.97 0.18 . 1.76 8.00 . Gardner Webb Campbell 5.00 6.83 7.78 7.97 7.61 7.37 6.69 6.70 10.00 6.32 7.90 5.90 5.90 3.00 3.85 6.50 . 9.00 3.00 Holy Cross Boston -11.50 -9.31 -6.21 -6.22 -6.31 -5.22 -7.83 -16.70 -15.00 -11.29 -11.62 -13.40 -8.90 -1.00 -9.16 -3.15 . . . .
The formatting doesn't quite show the problem, some of the numbers are separated by one space and some are separated by two spaces.
Here is my code to split that does not capture the items separated by one space.
Regex regex = new Regex("\\s{2,}");
string[] bits = regex.Split(tempStr[i]);
I need to fill and array with the same number of elements in each array.
How do I split the string into array elements when I have both 1 space or 2 spaces?
In addition, why this question is different from similar questions. I don't want to split between "Fresno" and "St."
So can I split the string differently for the first two fields which contain "legitimate" elements that contain spaces?
Solution:
It may not be the best but it works, I ended up using String.Substring().