Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Feb 2006
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How to find character in a String

    This has probably been asked before, and I apologize for asking again.

    Say I have a 1-character String (I can switch to char if it makes things easier), and I want to know if the String contains "0" - "9", is there something like variable.contains("0123456789") that I can use?

    I've tried searching, but I don't know the keywords. I'm still searching right now though. Sorry for the newb-ness. ^^

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Code:
    Character.isDigit("0".charAt(0)); // true
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • Users who have thanked shyam for this post:

    Katx18 (02-01-2008)

  • #3
    Regular Coder
    Join Date
    Jan 2008
    Location
    Willow Grove, PA
    Posts
    169
    Thanks
    1
    Thanked 27 Times in 27 Posts
    http://java.sun.com/j2se/1.4.2/docs/...x/Pattern.html

    The \d control matches a single digit. Read the javadoc on Pattern for more info.

    Code:
    Pattern p = Pattern.compile("\\d"); //double backslash to escape the escape character
    p.matcher("0").matches();  //true
    p.matcher("9").matches();  //true
    p.matcher("99").matches();  //false
    p.matcher("a").matches();  //false

  • #4
    New Coder
    Join Date
    Feb 2006
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi!
    I used the charAt & isDigit method.

    Thank you so much, both of you


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •