Reputation: 41
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
How do I parse this out below?
"Total Posts: 5,203"
Upvotes: 3
Views: 161
Reputation: 1677
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
.
Upvotes: 4
Reputation: 13
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
Upvotes: 0
Reputation: 136002
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(",", ""));
Upvotes: 2