如何使用getText()后比较字符串值到另一个字符串?



使用getText()方法提取tooltiptext时,输出有三行:

outside 
temperature
20c

但是如何比较提取的20c和另一个字符串30c呢?

我的方法:

if (tooltiptext.getText() > 30c) {
System.out.println("temperature is too low outside");
} else {
System.out.println("temperature is hot outside");
}

应该这样做:

String tooltipText = "outsidentemperaturen20c";
// ensure that we parse only the last line, so if digits appear in the other lines, there is no impact
String toolipTempText = tooltipText.split("n")[2];
int tempInCelsius = Integer.parseInt(toolipTempText.replaceAll("[^0-9]", ""));
if (tempInCelsius > 30) {
System.out.println("temperature is too low outside");
} else {
System.out.println("temperature is too hot outside");
}

您可以尝试以下操作:

int tempInCelsius = Integer.parseInt(getText().replaceAll("(?s).*?\b(\d+)c\b.*", "$1"));

相关内容

最新更新