tags:

views:

35

answers:

2
    Regex rx = new Regex(@"[+-]");
    string[] substrings = rx.Split(expression);

expression = "-9a3dcbh-3bca-4ab4cf-3hc" //This is the iput string I want to split that string between + or -. My VS debugger shows substring array like this: substrings[0] = null //???Why substrings[1] = 9a3dcbh substrings[2] = 3bca substrings[3] = 4ab4cf substrings[4] = 3hc

Why is the first element of arry null, is it because I am matching +-, and there is no + in my input string?

A: 

Because split eliminates the delimiter, and returns the string before and after the delimiters, in this case there are no characters before the delimiter, thus the first value is the empty string.

John Weldon
I see, thank you
dontoo
A: 

C# Regex.Split - Subpattern returns empty strings. The first answer has a great explanation

You could try this:

     string split_string = "-3243+324-32-2343";
     string[] nums = split_string.Split(new char[] { '-', '+' },
                                        StringSplitOptions.RemoveEmptyEntries);
SwDevMan81
I did something like that
dontoo