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++){
      System.out.println(words[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 }.

[{}()\[\]\s]*,[{}()\[\]\s]*|[{}()\[\]\s]+

As Java literal:

"[{}()\\[\\]\\s]*,[{}()\\[\\]\\s]*|[{}()\\[\\]\\s]+"

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=400720&siteId=1