Home > Software design >  Get two different delimiters from same string
Get two different delimiters from same string

Time:04-15

How can I use split function in java using two delimiters in the same string I want to get the words with commas and spaces separately

String I = "hello,hi hellow,bye"

I want to get the above string splited as

String var1 = hello,bye
String var2 = hi hellow

Any suggestion is very much valued.

CodePudding user response:

I would try to first split them with one of the delimiters, then for each resulting substring split with the other delimiter.

CodePudding user response:

In the example you provided you can split first on space then take the left (0) and right (1) elements

    String input = "hello,hi hellow,bye";

    String[] parts = input.split(" ");
    String var1 = parts[0];
    String var2 = parts[1];
  •  Tags:  
  • java
  • Related