[英]Parsing a string with letters and numbers
Running this method gives a string of total post output 运行此方法会提供一个总后置输出的字符串
String numberOfPost = test.runNewAdvancedSearch(query, waitTime, startDate, endDate, selectedBrowser, data1, "");
int numberOfPostsInt = Integer.parseInt(numberOfPosts.replace(",", ""));
this parseInt does not work 这个parseInt不起作用
How do I parse this out below? 我如何在下面解析这个?
"Total Posts: 5,203"
Try this fully functional example: 试试这个功能齐全的例子:
public class Temp {
public static void main(String[] args) {
String s = "Total Posts: 5,203";
s = s.replaceAll("[^0-9]+", "");
System.out.println(s);
}
}
Meaning of the regex pattern [^0-9]+
- Remove all characters which occurs one or more times +
which does NOT ^
belong to the list [ ]
of characters 0
to 9
. 正则表达式模式的含义
[^0-9]+
-删除时发生一次或多次的所有字符+
不^
属于列表[ ]
的字符0
至9
。
if comma is decimal separator: 如果逗号是小数分隔符:
double d = Double.parseDouble(s.substring(s.lastIndexOf(' ') + 1).replace(",", "."));
if comma is grouping separator: 如果逗号是分组分隔符:
long d = Long.parseLong(s.substring(s.lastIndexOf(' ') + 1).replace(",", ""));
Try This: 试试这个:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.