1

How do I split the string using forward slash? I've tried with following code:

I have string String x="10/20//30;expecting values 10,20,30.

When I tried to split using x.split("/"); then it only splitting 10,20,"",30
When I tried to split using x.split("//"); then it only splitting 10/20,30.

Please help me to split correctly. Is there any way to skip the one slash if there are 2 slashes present?

Regards, murali

Reimeus
  • 158,255
  • 15
  • 216
  • 276
user3376818
  • 61
  • 2
  • 8

4 Answers4

7

You could do

String[] array = x.split("/+");
Reimeus
  • 158,255
  • 15
  • 216
  • 276
1

Because I can't comment on previous answer:

I think it has to be like

String[] s = x.split("/+");

Because otherwise it would be a conversion from String Array to String, wouldn't it?

FatalMerlin
  • 1,463
  • 2
  • 14
  • 25
0

Try to use your first approach and remove empty value from the result, in example by using the solution described here: Remove Null Value from String array in java

Community
  • 1
  • 1
Karol Tyl
  • 338
  • 2
  • 8
0

It's splitting fine when you use x.split("/") the problem is it is inserting a blank where the // slash is because of how the split works it will return an array of [10, 20, , 30]. Just either remove all blanks from the array or when you process just skip elements that are blank.

brso05
  • 13,142
  • 2
  • 21
  • 40