如何将字符串转换为有条件的前pression在Java(Android版)? [英] how to convert a string into a conditional expression in java (Android)?
问题描述
我有一个字符串A> B,我想转换成普通有条件的前pression。
如何做到这一点的 Android版表示只使用JSE。
I have a String "a>b" and I want to convert into a normal conditional expression. How to do this in Android means using only JSE.
String abc = "a>10";
if(abc){
// TO SOME TASK
}
任何帮助将AP preciated。
Any help will be appreciated.
感谢你在前进!
推荐答案
我会建议使用一个返回boolean结果的帮手的方法,这样的声明会是这个样子。
I would recommend using a "helper" method that returns a boolean result, so the statement would look something like this
String abc = "a>10";
if(stringToConditional("a>b"))
{
//TO SOME TASK
}
private boolean stringToConditional(String str)
{
//code here
}
从那里,助手的方法可以将字符串使用正则表达式分为三部分。乐于助人,教唆密,教程,可以发现这里。从那里,a和b可以用约翰内斯堡方法进行解析。它看起来是这样的:
From there, the "helper" method can split the string into three parts using regex. A helpful, abet dense, tutorial can be found here. From there, a and b can be parsed using JSE methods. It looks something like this:
String a = "1.21";
double a1 = Double.parseDouble(a); //a1 is now equal to 1.21
从那里,下一个步骤将是比较条件字符串(>,&下; =,==等),以公知的运算符的列表,然后检测该操作并返回结果作为方法的布尔返回。例如:
From there, the next step would be to compare the conditional string (">", "<=", "==", etc.) to a list of known operators, and then testing the operation and returning the result as the method's boolean return. For example:
String operator = ">=";
if (operator.equals(">="))
{
return a1>=b1;
}
...
希望我是不是太不明确或没谱。我很高兴来阐述,澄清或简化。祝你好运!
Hopefully I wasn't too unclear or off the mark. I'd be happy to elaborate, clarify or simplify. Good Luck!
这篇关于如何将字符串转换为有条件的前pression在Java(Android版)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!