0

I'm trying to split the characters of the String to a String array. I found the solution here.

The solution is perfect, however I don't get how .split("(?!^)") worked. I'm familiar with the basics of split() method. Can someone provide an explanation?

4

1 回答 1

7

(?!^) is a regular expression consisting of a negative lookahead. ^ is an anchor used to signify the start of the string. (?!^) matches all 0-length strings that are not followed by ^, the start of the string. In other words, it matches all 0-length strings except that at the start of the string.

For example, in the string abc, there will be 3 matches: one between a and b, one between b and c, and one after the c. Splitting on these matches produces the desired array (note that the 1-argument version of split() discards any trailing empty strings, which is why none are included in the resulting array).

于 2013-09-08T21:26:54.200 回答