无法从字符串中删除空格

问题描述 投票:0回答:1

我正在本机android应用上使用appium自动化

我有一个想要从中获取文本的MobileElement

MobileElement likesElement = (MobileElement) driver.findElement(By.id("com.instagram.android:id/row_feed_textview_likes"));

然后我有了字符串

String likesVal = likesElement.getText();

当我运行System.out.println(likesVal);时会打印:

Liked by you and 107 others

我需要删除除107以外的所有内容,以便可以在likesVal上运行parseInt

我已经尝试过

likesVal= likesVal.replaceAll("[*a-zA-Z_.-]", "");
likesVal= likesVal.replaceAll(" ", "");
StringUtils.deleteWhitespace(likesVal);
int likes = Integer.parseInt(likesVal);
System.out.println(likes);

但是我在java.lang.NumberFormatException For input string: "107 "上得到了int likes = Integer.parseInt(likesVal);

除去数字后的likesVal之外的所有内容,为什么不删除likesVal中“其他”之前的空格?

除去数字后的所有内容likesVal之后,我需要打印“ 107”而不是“ 107”

java string replace appium removing-whitespace
1个回答
0
投票
我建议使用模式来替换所有非数字(\\D)。喜欢,
© www.soinside.com 2019 - 2024. All rights reserved.