How exactly does String.split() method in Java work when regex is provided?

与世无争的帅哥 提交于 2019-11-27 02:01:40
Pshemo

It splits on every "\\S" which in regex engine represents \S non-whitespace character.

So lets try to split "x x" on non-whitespace (\S). Since this regex can be matched by one character lets iterate over them to mark places of split (we will use pipe | for that).

  • is 'x' non-whitespace? YES, so lets mark it | x
  • is ' ' non-whitespace? NO, so we leave it as is
  • is last 'x' non-whitespace? YES, so lets mark it | |

So as result we need to split our string at start and at end which initially gives us result array

["", " ", ""]
   ^    ^ - here we split

But since trailing empty strings are removed, result would be

[""," "]     <- result
        ,""] <- removed trailing empty string

so split returns array ["", " "] which contains only two elements.

BTW. To turn off removing last empty strings you need to use split(regex,limit) with negative value of limit like split("\\S",-1).


Now lets get back to your example. In case of your data you are splitting on each of

I am preparing for OCPJP
| || ||||||||| ||| |||||

which means

 ""|" "|""|" "|""|""|""|""|""|""|""|""|" "|""|""|" "|""|""|""|""|""

So this represents this array

[""," ",""," ","","","","","","","",""," ","",""," ","","","","",""]  

but since trailing empty strings "" are removed (if their existence was caused by split - more info at: Confusing output from String.split)

[""," ",""," ","","","","","","","",""," ","",""," ","","","","",""]  
                                                     ^^ ^^ ^^ ^^ ^^

you are getting as result array which contains only this part:

[""," ",""," ","","","","","","","",""," ","",""," "]  

which are exactly 16 elements.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!