Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Get number from string (like ""Wintertodt's Energy: 35%") using regex


    Recommended Posts

    Someone asked about this in discord earlier today, and I helped them get it working. I figured I would post it here in case anyone else has the same issue. Basically, he needed to get the % value from the string "Wintertodt's Energy: 35%". To do this, I used a regular expression to grab only the numbers from the string, and then used `parseInt` on the result. Here is an example on how to do this.

            // this should be replaced with whatever code fetches the string
            String str = "Wintertodt's Energy: 35%";
            // create the regex pattern to find numbers
            Pattern ints = Pattern.compile("\\d+");
            // run the regex pattern on the string we want to parse the numbers from
            Matcher makeMatch = ints.matcher(str);
            // set the result
            String result = makeMatch.group();
            // this should print 35 in your console (note that 35 is actually still a string at this point)
            // parse the number from the result string to convert it to the right data type (int)
            int integerValue = Integer.parseInt(result);
            // this should print 35 in your console (note that 35 is an int here, which is why we have to do integerValue+"" to get it to log since log takes in a string)

    Once you have `integerValue`, you can do things like `if (integerValue > 50)` or `if (integerValue == 100)`. Let me know if you have any questions or suggestions on an easier way to do this.


    Here are the required imports for the code above

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    Link to comment
    Share on other sites


    This topic is now archived and is closed to further replies.

    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.