运行此方法会提供一个总后期输出字符串
String numberOfPost = test.runNewAdvancedSearch(query, waitTime, startDate, endDate, selectedBrowser, data1, "");
int numberOfPostsInt = Integer.parseInt(numberOfPosts.replace(",", ""));
此parseInt不起作用
我如何在下面解析这个?
"Total Posts: 5,203"
答案 0 :(得分:4)
试试这个功能齐全的例子:
public class Temp {
public static void main(String[] args) {
String s = "Total Posts: 5,203";
s = s.replaceAll("[^0-9]+", "");
System.out.println(s);
}
}
正则表达式模式的含义[^0-9]+
- 删除+
一次或多次出现的所有字符,其中^
不属于字符[ ]
0
到9
。
答案 1 :(得分:2)
如果逗号是小数分隔符:
double d = Double.parseDouble(s.substring(s.lastIndexOf(' ') + 1).replace(",", "."));
如果逗号是分组分隔符:
long d = Long.parseLong(s.substring(s.lastIndexOf(' ') + 1).replace(",", ""));
答案 2 :(得分:0)
试试这个:
String numberOfPost = test.runNewAdvancedSearch(query, waitTime, startDate, endDate, selectedBrowser, data1, ""); // get the parse string "Total Posts: 5,203
int index = numberOfPost.lastIndexOf(":");
String number = numberOfPost.substring(index + 1);
int numOfPost = Integer.parseInt(number.replace(",", "").trim());
System.out.println(numOfPost); // 5203enter code here