文字列を区切り文字に従って分割したいのですが、括弧の外側のみです。そうするライブラリ(組み込みかどうか)はありますか?例:区切り文字が「:」の場合:文字列「a:b:c」は「a」、「b」、「c」に分割する必要があります文字列「a(b:c):d」は「a」に分割する必要があります(b:c) "、" d "
ありがとう
他のコメンターは、文法ライブラリを使用するのがおそらく最善であるということは正しいです。ただし、これが 1 回限りのものであり、迅速に処理したい場合、このアルゴリズムは明確な方法で処理する必要があり、ネストされた括弧を処理します。注: 括弧のバランスが取れていると思います。つまり、左括弧の前に左括弧がない右括弧はありません。
int parenDepth = 0;
int start = 0;
List<String> splits = new ArrayList<String>();
for(int i = 0; i < str.length(); i++)
{
char ch = str.get(i);
if(ch == '(')
parenDepth++;
else if(ch == ')')
parenDepth--;
else if(parenDepth == 0 && ch ==',')
{
if(start != i) // comment out this if if you want to allow empty strings in
// the splits
splits.add(str.substring(start, i));
start = i+1;
}
}
splits.add(str.substring(start));