0
String source = "s5 g900 sued_p033178672__.____ ____.__4.5cm"; // __ __ is not a delimiter 

String deli = "__.__"; // this is my separator string to split target

String[] splittedString = source.split(deli, -1);

splittedString.length; // I expected 3 but was 4

what should I do to split target string properly?

1 Answer 1

1

String.split() takes a regular expression as its input. Therefore, . matches any characters, so "__ __" is treated as a delimiter as well.

To make it matches only "__.__", you need to escape the special character ., i.e.,

String deli = "__\\.__";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.