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()?

Jason Z
  • 265
  • 1
  • 6
  • 12

1 Answers1

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("\\.+");
Reimeus
  • 158,255
  • 15
  • 216
  • 276