String.replace()返回不需要的字符串 [英] String.replace() returns unwanted string
问题描述
我正在处理一段代码,我要将字符串拆分成单个部分并替换它们。 我的代码的基本逻辑流程是,有一个包含公式的字符串。 LHS下面的数字,即1,2和3是不同物体的ID。一旦我拆分它们,我就会使用这些ID,获取相应的值,并将下面String中的ID替换为各自的值。我所拥有的字符串如下 -
I'm working on a piece of code where I've to split a string into individual parts and replace them. The basic logic flow of my code is, there's a string that contains a formula. The numbers below on the LHS, i.e 1, 2 and 3 are ids of different objects. Once I split them, I'd use these ids, get the respective value and replace the ids in the below String with its respective values. The string that I have is as follow -
String str = "(1+2+3)>100";
我使用以下代码分割字符串 -
I've used the following code for splitting the string -
String[] arraySplit = str.split("\\>|\\<|\\=");
String[] finalArray = arraySplit[0].split("\\(|\\)|\\+|\\-|\\*");
arraySplit[0] = arraySplit[0].replace(id,reading);
因此,在拆分字符串后,我会用值替换字符串,即字符串现在是,(30 + 45 + 50)> 100
其中30,45和50是相应的值。 (此字符串将在SpEL中用于评估公式)
So, after the string is split, I'd replace the string with the values, i.e the string would now be, (30+45+50)>100
where 30, 45 and 50 are the respective values. (this string would then be used in SpEL to evaluate the formula)
在用值替换字符串时,我得到以下内容回复 -
While replacing the string with the values, I'm getting the following response -
初始字符串 -
(1 + 2 + 3)> 100
其中1,2和3是ID。 id的各个值分别为30.0,45.0和50.0。
(1+2+3)>100
where 1, 2 and 3 are ids. Respective values of the ids are 30.0, 45.0 and 50.0.
使用for循环替换值后,字符串为 -
After replacing the value by using a for loop, the string is -
(50.00.0 + 45.0 + 50.0)> 100
。第一个值很奇怪。原因是在第三次迭代中,字符串看起来像这样 - (30.0 + 45.0 + 3)> 100。因此,在第三次迭代中替换字符串时,将所有3的实例替换为50.0 ,从而产生上述字符串。所以显然使用 String.replace()
在这里不是更好的选择。那么在这种情况下我应该使用什么方法呢?我也在使用SpEL,所以如果使用SpEL有更好的解决方案,那么它也很好。
(50.00.0+45.0+50.0)>100
. The first value is quite weird. The reason for this is while in the third iteration, the string looked like this - (30.0+45.0+3)>100. So while replacing the string in the third iteration, it replaces all instances of 3 with 50.0 hence resulting in the above string. So apparently using String.replace()
won't be a better option here. What method should I use in this case then? I'm also using SpEL, so if there's a better solution to this using SpEL then also its good.
这里,(1 + 2 + 3)> 100
只是一个例子。圆括号是公式的一部分,字符串也可以是((1 + 2 + 3)*(5-2))> 100
。或者甚至可以改变公式的语法,目前公式是这样的 - (1 + 2 + 3)* 4> 100 但是如果通过改变公式语法有一点出路然后这也有帮助,例如用 - ({#1} + {#2} + {#3})*
{#4}> 100 替换公式,在这种情况下我使用{#作为变量得到变量并得到数字。
Here, (1+2+3)>100
is just an example. The round braces are part of a formula, and the string could also be as ((1+2+3)*(5-2))>100
. Or even the syntax of the formula could be changed, currently the formula is as such - (1+2+3) * 4>100 but if there's a way out by changing the formula syntax a bit then that would also be helpful, e.g replacing the formula by - ({#1}+{#2}+{#3}) *
{#4}>100, in this case I'd get the variable using {# as the variable and get the numbers.
我希望这部分是明确的。
I hope this part is clear.
推荐答案
你没错, String.replace
不适合这里的工作。
You're right, String.replace
is not the right tool for the job here.
您要做的是标记化输入字符串,然后对标记进行操作。您可以选择在最后组合回一个字符串,但这取决于您打算如何处理公式。
What you want to do is to tokenize the input string, then operate on the tokens. Optionally you can combine back into a string at the end, but it depends on what you intend to do with the formula.
例如,您想要标记字符串(1 + 2 + 3)> 100
进入数组 [(,1,+,2, +,3,),>,100]
。这只是在数字和符号之间的每个边界上分裂的问题。
For example, you want to tokenize the string "(1+2+3)>100"
into the array ["(", "1", "+", "2", "+", "3", ")", ">", "100"]
. This is simply a matter of splitting on every boundary between a digit and a symbol.
从这个表示中,用它们替换数组中的整个标记是一件简单的事情。相应的值,例如找到值1
的数组索引,并将其替换为30.0
等。然后你可以创建一个新的表达式字符串,只需将数组中的所有部分连接在一起。
From this representation, it's a simple matter to replace the entire tokens in the array with their corresponding values, e.g. find the array index that has value "1"
and replace it with "30.0"
, etc. Then you can create a new expression string by just joining all the parts in the array back together.
如果您想更进一步,可以转换为表达式树,它看起来像这样:
If you want to go one step further, you can translate into an expression tree, which would look something like this:
OpGreaterThan
OpPlus
Symbol(1)
OpPlus
Symbol(2)
Symbol(3)
Literal(100)
这个树是通过将输入表达式(以标准中缀表示法呈现)转换为反向抛光表示法,然后构建一个树,其中每个节点是一个运算符,其子节点是操作数。这将允许您象征性地操纵和/或评估公式。
This tree is formed by transforming the input expression (which is rendered in standard infix notation) into reverse polish notation, then building a tree where each node is an operator and its children are operands. This would allow you to symbolically manipulate and/or evaluate the formula.
这篇关于String.replace()返回不需要的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!