Why am I receiving a length of 3 instead of 4? How can I fix this to give the proper length?
String s="+9851452;;FERRARI;;";
String split[]=s.split("[;]");
System.out.println(split.length);
You're receiving a length of 3 because for split
,
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
If you specify a negative limit, it'll work fine:
String s="+9851452;;FERRARI;;";
String split[]=s.split(";", -1);
System.out.println(Arrays.toString(split));
You'll just need to ignore or remove the 5th item, or remove the trailing ;
- it shows up because there are 5 (potentially blank) strings on either sides of 4 tokens. See the docs for more info.
String split[]=s.split("[;]", -1);
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