Strange, regex.split Method corresponds to one null element

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

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

Why is the first element arry null, is it because I match + - and there is no input in my input line?

+4
source share
2 answers

Since split removes the delimiter and returns the line before and after the delimiters, in this case there are no characters before the delimiter, so the first value is an empty string.

+1
source

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

You can try the following:

  string split_string = "-3243+324-32-2343"; string[] nums = split_string.Split(new char[] { '-', '+' }, StringSplitOptions.RemoveEmptyEntries); 
0
source

Source: https://habr.com/ru/post/1308173/


All Articles