0

I wish to extract the numerical part of a text in string and convert it to int. For example, consider the following text :

1-16 of 310 results for "phone case"

Normally, I could use

String total_item_str = search_result.substring(8,10);

to extract the value 310. However, that is if it is consistently 3 digits.

My issue is since this is returning the total search result and it can any number of digits, how do handle this dynamically ? Meaning, I wish to be able to extract the total search result from the text no matter how many digits it returns.

4
  • 1
    Try regular expressions Commented Mar 9, 2021 at 11:16
  • 1
    Is the number you want always in the form of "of XXX results"? If yes you could simply grab the part between "of" and "results" with something like String numberofResults = string.substring(string.indexOf("of")+2, string.indexOf("result")).trim(); and then just parse that String to an Integer. Commented Mar 9, 2021 at 11:16
  • @OH GOD SPIDERS Unfortunately, it could be int he form of X, XX, XXX or even XXXX, depending on the total number of items being found. WHat is the puorpose of the +2 ? Commented Mar 9, 2021 at 11:18
  • @crissal You mean something like \d ? Commented Mar 9, 2021 at 11:20

2 Answers 2

5

You can apply a simple regular expression:

String result = "1-16 of 310 results for \"phone case\"";
        
Pattern pattern = Pattern.compile("\\S* of (\\d+) result.+");
Matcher matcher = pattern.matcher(result);
if (matcher.matches()) {
    int totalCount = Integer.parseInt(matcher.group(1));
    ...
}
Sign up to request clarification or add additional context in comments.

3 Comments

@M A, I'll give it a try. Thanks for sharing ! By the way, what does "(matcher.group(1)" do ?
I propose to change the first .+ to \\S* to limit backtracking.
@Tester_at_work It returns the first group (delimited by parentheses) that was captured by the match: docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/…
0

Another possibility is to use replaceAll to eliminate everything but the target digits. The $1 is a back reference to the capture group in the string.

String[] data = {
    "1-16 of 1 results for \"phone case\"",
    "1-16 of 12 results for \"phone case\"",
    "1-16 of 123 results for \"phone case\"",
    "1-16 of 1234 results for \"phone case\"",
    "1-16 of 12345 results for \"phone case\"",
    "1-16 of 123456 results for \"phone case\""};
for (String s : data){
    int v = Integer.parseInt(s.replaceAll("\\S* of (\\d+) results.*", "$1"));
    System.out.printf("%-6d : %s%n", v, s);
}

prints

1      : 1-16 of 1 results for "phone case"
12     : 1-16 of 12 results for "phone case"
123    : 1-16 of 123 results for "phone case"
1234   : 1-16 of 1234 results for "phone case"
12345  : 1-16 of 12345 results for "phone case"
123456 : 1-16 of 123456 results for "phone case"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.