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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With