For example
String splits[] = ("||").split("[|]")
System.out.println(splits.length) returns 0. Whereas I expected it to return 2. Whats the best way to solve this problem ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The default operation of
split()is to discard trailing empty strings. Since every resulting string is empty, no strings will be returned.More precisely, calling
split(String)is the same as callingsplit(String, int)with the second parameter set to 0.And 0 means (emphasis mine):
If you want to keep trailing empty strings (and don’t want to limit the number of elements), then pass in a negative value (I’d suggest -1):