i want to split my java string array when multiple charactors in nearly in my string

Rayan9X :

i've used .split function for do the task but as i noticed when there are two special charactors in near my array filled with a space here is my code

String strLine="cat dog man()home(String[welcome)throws , guess {";
String[] words = strLine.split("\\{+|\\(+|\\[+|\\]+|\\)+|\\,|\\s+");
int len=words.length;

for(int x=0;x<len;x++){

result showing asenter image description here

i want my array without spaces any suggestions

Andreas :

Since you want adjacent separator characters to be treated as a single separator, just combine them in a single character class.

\{+|\(+|\[+|\]+|\)+|\,|\s+     →     [{(\[\]),\s]+

However, since you don't have a + on the ,, it seems that multiple commas should not be treated as a single separator, so we need to handle that differently.

It also seems like you forgot the }.


As Java literal:


Guess you like