-2

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.

enter image description here

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().

6
  • 1
    Sorry what is the question? Commented Jan 5, 2021 at 3:09
  • 1
    A better idea is to spit on 1 space, then trim each result, and ignore any nulls or empties. Commented Jan 5, 2021 at 3:44
  • Dim splits = input.Split({" "c}, StringSplitOptions.RemoveEmptyEntries) This didn't have any problem with spaces but I got a few elements with nothing but a dot. Commented Jan 5, 2021 at 3:50
  • you can use string split function with to define both double and single space as separator --- string[] seperators = {" ", " "}; var split = str.Split(seperators, StringSplitOptions.RemoveEmptyEntries); Commented Jan 5, 2021 at 3:51
  • Added example that shows "Fresno" "St." should not be split. Commented Jan 5, 2021 at 15:17

1 Answer 1

0

Find the right rexex was to difficult/complicated.

Instead I used SubString . Below is a partial snippet of the code I used.

 string temp1 = foo.Substring(0, 10);
                            al.Add(foo.Substring(0, 19));
                            al.Add(foo.Substring(20, 19));
                            al.Add(foo.Substring(42, 7));
                            al.Add(foo.Substring(49, 7));
                            al.Add(foo.Substring(56, 7));
                            al.Add(foo.Substring(63, 7));
                            al.Add(foo.Substring(70, 7));
                            al.Add(foo.Substring(77, 7));
                           
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.