Case insensitive String split() method

前端 未结 6 1597
北海茫月
北海茫月 2020-12-10 01:48

When I perform

String test=\"23x34 \";
String[] array=test.split(\"x\"); //splitting using simple letter

I got two items in array as 23 and

6条回答
  •  [愿得一人]
    2020-12-10 02:28

    split uses, as the documentation suggests, a regexp. a regexp for your example would be :

    "[xX]"
    

    Also, the (?i) flag toggles case insensitivty. Therefore, the following is also correct :

    "(?i)x"
    

    In this case, x can be any litteral properly escaped.

提交回复
热议问题