Home > Software design >  Split a String but remove delimeters only and keep spaces - java
Split a String but remove delimeters only and keep spaces - java

Time:04-15

I have a string like this:

String str = "SOFTDRINK,SD03,Lemon juice,lemon juice,lemonjuice.img,30000.0";

// How can i get this expected string array:
String[] expected = ["SOFTDRINK","SD03","Lemon juice","lemonjuice","lemonjuice.img","30000.0"];

I use .split(",") and iterate the array, it would be a errors at "Lemon juice" where there is a space.

UPDATE: No error of .split(",") faced. My String str wasn't returned as shown above so I faced errors related to it.

CodePudding user response:

i dont think you should face any error here

anyways try this , you will get your expected output

    String str = "SOFTDRINK,SD03,Lemon juice,lemon juice,lemonjuice.img,30000.0";
    String[] expected = str.split(",");
    System.out.println(Arrays.toString(expected));

even if it doesnt work out add more of your code

  • Related