Java 如何拆分以逗号分隔的字符串,该字符串包含带逗号的括号内的子字符串

Java 如何拆分以逗号分隔的字符串,该字符串包含带逗号的括号内的子字符串,java,Java,以下是我的例子: 输入: String str="hello,there,(what,is,new),today,(with,you)" 期望输出: hello there (what,is,new) today (with,you) 我认为最简单的方法是在for循环中逐个字符地遍历字符串,并用空格替换逗号,除非您在括号内(您需要保留它们的数量)现在它可以工作了 String str="hello,there,(what,is,new),today,(with,you)"; int insi

以下是我的例子:

输入:

String str="hello,there,(what,is,new),today,(with,you)"
期望输出:

hello
there
(what,is,new)
today
(with,you)

我认为最简单的方法是在for循环中逐个字符地遍历字符串,并用空格替换逗号,除非您在括号内(您需要保留它们的数量)

现在它可以工作了

String str="hello,there,(what,is,new),today,(with,you)";
int inside = 0;
int index = 0;
for (int i = 0; i < str.length(); i++){
    if (str.charAt(i) == ')'){
        inside--;
    }else if (str.charAt(i) == '('){
        inside++;
    }else if (str.charAt(i) == ',' && inside == 0){
        System.out.println(str.substring(index, i));
    index = i+1;
    }
}if (index != str.length()){
    System.out.println(str.substring(index, str.length()));
}
String str=“你好,(什么,是新的),今天,(和你一起)”;
内部整数=0;
int指数=0;
对于(int i=0;i
ArrayList myList=new ArrayList();
int index1=-1;
int index2=0;
对于(int i=0;i
String str=“你好,这里,(什么,是新的),今天,(和你一起)”;
ArrayList myList=新的ArrayList();
int index1=-1;
int index2=0;
对于(int i=0;i
String str="hello,there,(what,is,new),today,(with,you)";
ArrayList<String> myList = new ArrayList<String>();
int index1=-1;
int index2=0;
for(int i=0;i< str.length();i++)
{
if(str.charAt(i) != '(' ){
   if(str.charAt(i) == ',' )
     {
  index2=i;
  myList.add(str.substring(index1+1 ,index2));
  index1=index2;
  }
}
else
    while(str.charAt(i)!=')')
        i++;
}

for(int i=0;i<myList.size();i++) System.out.println(myList.get(i));