0

I have an if statement which checks if a variable is equal to some string. But, I want to check if there is an number in the string, too. Something like this:

if(thestring.equals("I, am awesome. And I'm " + Somehowgetifthereisanumberhere + " years old")) {
    //Do stuff
}

Or more specifically, where x is the unknown number, just to know there is a number (any number) there:

String str = item.substring(item.indexOf("AaAaA" + x), item.lastIndexOf("I'm cool."));

How to do that?

user
  • 6,897
  • 8
  • 43
  • 79
GuiceU
  • 1,030
  • 6
  • 19
  • 35
  • Looked in SO..I think this has been asked: http://stackoverflow.com/questions/372148/regex-to-find-an-integer-within-a-string – Menelaos May 21 '13 at 18:47

4 Answers4

5

Use a regular expression:

if(thestring.matches("^I, am awesome. And I'm \\d+ years old$")) {
    //Do stuff
}
user
  • 6,897
  • 8
  • 43
  • 79
Cairnarvon
  • 25,981
  • 9
  • 51
  • 65
  • Also note that the double backslash is because the double backslash \\ translates to a single backslash within the string, and then the sequence \d is seen and parsed by the regular expression engine. – user May 21 '13 at 18:42
  • That's awesome, but can I somehow put this in my substring thing? Really thankful for your help :) – GuiceU May 21 '13 at 18:44
  • @GuiceU You don't; this would replace your `thestring.equals()` call. – user May 21 '13 at 18:45
  • @MichaelKjörling Yes, I know. So I cannot use this somehow in my substring? Is there anything else I can use then? EDIT: Or could'nt I just use the \\d+? – GuiceU May 21 '13 at 18:48
  • @GuiceU \\d+ (actually, \d+ - see my comment above about the double backslash) is regular expression syntax, so no, you cannot use it with `String.equals()`. However, you *can* use it with `String.matches()` as Cairnarvon showed in the answer. It *is* possible to do this without regular expressions, but I'd say they are by far the best suited tool for this kind of string processing. – user May 21 '13 at 18:50
  • The regex pattern \d matches any digit, and + means repeat the previous pattern for as long as possible (otherwise you'd match only a single digit). There's more to it than that if you dig into it all, but that's the gist of it. So by having \d+ in the match expression you will match one or more digits in that position, which is what you wanted to do. – user May 21 '13 at 18:52
  • @MichaelKjörling Dude, _thank you_! Seriously, you're awesome. Now to the question. Okey, I get that, so it isn't possible to use it with 'String.indexOf()' either? If not I'll just have to find another way to do this, but again. Thank you :) – GuiceU May 21 '13 at 19:24
  • @GuiceU If you need the position within the string as well, [Penelope's answer](http://stackoverflow.com/a/16677427/486504) is probably the best way to do it. But that wasn't in the original question. Also, you may want to try [searching for questions tagged java regex](http://stackoverflow.com/questions/tagged/java+regex). – user May 21 '13 at 19:31
  • @MichaelKjörling Well, doesn't work in my case. But thank you all anyway! – GuiceU May 22 '13 at 18:30
2

You can use the regular expression.

Pattern pattern = Pattern.compile(".*[^0-9].*");

References:

Community
  • 1
  • 1
Menelaos
  • 23,508
  • 18
  • 90
  • 155
2

This regex should find any one, two or three digit number (in case they are 102 years old) within any string:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TestClass {

public static void main(String[] args) {
    Pattern p = Pattern.compile("\\d\\d?\\d?");
    Matcher m = p.matcher("some string with a number like this 536 in it");
    while(m.find()){
        System.out.println(m.group());  //This will print the age in your string
        System.out.println(m.start());  //This will print the position in the string where it starts
    }
  }
}

Or this to test the entire string:

Pattern p = Pattern.compile("I, am awesome. And I'm \\d{1,3} years old");  //I've stolen Michael's \\d{1,3} bit here, 'cos it rocks.
Matcher m = p.matcher("I, am awesome. And I'm 65 years old");
    while(m.find()){
        System.out.println(m.group());
        System.out.println(m.start());
}
Penelope The Duck
  • 656
  • 1
  • 7
  • 16
1

You'd want to use regular expressions. See - Using Regular Expressions to Extract a Value in Java

To match letters 'd', 'e', or 'f', for e.g.:

[a-z&&[def]]   

And also - Lesson: Regular Expressions

Pattern class is good study too

Community
  • 1
  • 1
Caffeinated
  • 11,982
  • 40
  • 122
  • 216