Case insensitive String split() method

前端 未结 6 1601
北海茫月
北海茫月 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:41

    You could use a regex as an argument to split, like this:

    "32x23".split("[xX]");
    

    Or you could use a StringTokenizer that lets you set its set of delimiters, like this:

    StringTokenizer st = new StringTokenizer("32x23","xX");
    //                                          ^^    ^^
    //                                       string delimiter
    

    This has the advantage that if you want to build the list of delimiters programatically, for example for each lowercase letter in the delimiter list add its uppercase corespondent, you can do this and then pass the result to the StringTokenizer.

提交回复
热议问题