Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
111 views
in Technique[技术] by (71.8m points)

java - Case insensitive String split() method

When I perform

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

I got two items in array as 23 and 34

but when I did

String test="23x34 ";
String[] array=test.split("X"); //splitting using capitalletter

I got one item in array 23x34

So is there any way I can use the split method as case insensitive or whether there is any other method that can help?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...