Java字符串拆分为多个分隔符,用于布尔表达式

时间:2022-04-08 21:37:26

I want to split a string in Java that represents a boolean expression. The string is of the form:

我想在Java中拆分一个表示布尔表达式的字符串。字符串的形式如下:

key <operator> value

where operator can be "==", "!=", ">", "<"

运算符可以是“==”,“!=”,“>”,“<”

For example: Input1: a==2 output ["a", "2"] Input2: b!=3 output ["b", "3"]

例如:Input1:a == 2 output [“a”,“2”] Input2:b!= 3 output [“b”,“3”]

How do I achieve this ?

我该如何实现这一目标?

2 个解决方案

#1


1  

This should do it.

这应该做到这一点。

String[] arr;                       //create a string array to reference later
if (Input1.contains("=="))          //check string for measure
     arr = String.split("==");      //split string at those points
else if (Input1.contains("!="))     //a==2 -> ["a", "2"]
     arr = String.split("!=");
else if (Input1.contains(">"))
     arr = String.split(">");
else if (Input1.contains("<"))
     arr = String.split("<");

or:

要么:

String arr[] = Input1.split("!=|>|<|==");

Hope this helps.

希望这可以帮助。

#2


0  

In the regex part, list all of your signs separated by a single OR sign (|).

在正则表达式部分中,列出由单个OR符号(|)分隔的所有符号。

#1


1  

This should do it.

这应该做到这一点。

String[] arr;                       //create a string array to reference later
if (Input1.contains("=="))          //check string for measure
     arr = String.split("==");      //split string at those points
else if (Input1.contains("!="))     //a==2 -> ["a", "2"]
     arr = String.split("!=");
else if (Input1.contains(">"))
     arr = String.split(">");
else if (Input1.contains("<"))
     arr = String.split("<");

or:

要么:

String arr[] = Input1.split("!=|>|<|==");

Hope this helps.

希望这可以帮助。

#2


0  

In the regex part, list all of your signs separated by a single OR sign (|).

在正则表达式部分中,列出由单个OR符号(|)分隔的所有符号。