1. Are you ready for the Galaxy S20? Here is everything we know so far!

How can I verify if a string contains at least one number?

Discussion in 'Android Development' started by Federico Desia, Apr 1, 2019.

  1. Federico Desia

    Thread Starter

    How can I verify if a string contains at least one number?
     


  2. Best Answer:
    Post #2 by Deleted User, Apr 2, 2019 (1 points)
  3. Deleted User

    Deleted User Guest

    You could do it with a regex

    Code (Text):
    1.  
    2. String line = "te5t";
    3. Pattern r = Pattern.compile(".*[0-9].*");
    4. Matcher m = r.matcher(line);
    5. if (m.find( )) {
    6.   System.out.println("It matched");
    7. }
     
  4. scary alien

    scary alien not really so scary
    Moderator

    GameTheory and Deleted User like this.
  5. Deleted User

    Deleted User Guest

    Thanks Scary. I have to give 0.5 of that to @GameTheory who put the idea of regex in my head with an answer to a recent question.
     
    GameTheory and scary alien like this.
Loading...

Share This Page

Loading...