Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case insensitive String split() method

Tags:

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?

like image 559
Sanjaya Liyanage Avatar asked May 16 '13 08:05

Sanjaya Liyanage


People also ask

Is Split case-sensitive?

Using Split and Join The split() method converts the string into an array of substrings based on a specified value (case-sensitive) and returns the array. If an empty string is used as the separator, the string is split between each character.

What is split () function in string?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

How does case insensitive compare strings?

Comparing strings in a case insensitive manner means to compare them without taking care of the uppercase and lowercase letters. To perform this operation the most preferred method is to use either toUpperCase() or toLowerCase() function.

Are string cases insensitive?

It's case-sensitive. How can I change it so that it's not? If you know it is case sensitive, you could convert both to lowercase or uppercase before comparing.


1 Answers

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.

like image 187
njzk2 Avatar answered Oct 18 '22 04:10

njzk2