gpt4 book ai didi

java - 如何在java中拆分字符串中的多个运算符

转载 作者:行者123 更新时间:2023-12-02 05:52:28 24 4
gpt4 key购买 nike

我在java中有字符串,但不明白如何使用分割这些类型的字符串。

我只有算术和逻辑运算符

 char[] operators = new char[] { '\\', 'x', '+', '-', '>', '*','<', '=' };
String str_spit="usa_newyork=japan\london*44+jhon<last-987";

实际字符串->

 String a= QN_770_0=QN_770_0\10

 String b= QN_770_0>66

我的代码:

ArrayList <String> logics;
ArrayList <String> logicQuestions = null;

char[] operators = new char[] { '\\', 'x', '+', '-', '>', '<', '=' };

String str_spit="QN_770_0=QN_770_0\10";
for ( jj = 0; jj < operators.length; jj++)
{
System.out.println("operators.toString()---->"+operators[jj]);
//String[] questions = logicText.split(operators);
String s = "" + operators[jj];
String[] questions=str_spit.split(s);
System.out.println("questions questions---->"+questions);
//for each segment, save all question codes.
for (int j = 0; j < questions.length; j++)
{
String question = questions[j];
System.out.println("questions questions---question->"+questions);
if (question.startsWith("QN_") && !logicQuestions.contains(question))
logicQuestions.add(question);
System.out.println("logicQuestions---logicQuestions->"+logicQuestions);
}

}

错误:

operators.toString()---->\
Exception in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
^

最佳答案

尝试使用 StringTokenizer

    String delim = new String(operators);    
StringTokenizer st = new StringTokenizer(str_spit, delim);
while (st.hasMoreTokens()) {
System.out.println(st.nextToken());
}

关于java - 如何在java中拆分字符串中的多个运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23424742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com