6

I have a String like

String s="hello.are..you";
String test[]=s.split("\\.");

The test[] includes 4 elements:

hello
are

you

How to just generate three not empty elements using split()?

0

1 Answer 1

9

You could use a quantifier

String[] array = "hello.are..you".split("\\.+");

To handle a leading . character you could do:

String[] array = ".hello.are..you".replaceAll("^\\.",  "").split("\\.+");
Sign up to request clarification or add additional context in comments.

2 Comments

sorry, but what if the String is ".hello"
are you expecting the same output?

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.