...

View Full Version : How to find character in a String



Katx18
01-31-2008, 07:11 AM
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. ^^

shyam
01-31-2008, 08:28 AM
Character.isDigit("0".charAt(0)); // true

sobrien79
01-31-2008, 06:26 PM
http://java.sun.com/j2se/1.4.2/docs/api/java/util/regex/Pattern.html

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



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

Katx18
02-01-2008, 03:00 AM
Hi!
I used the charAt & isDigit method.

Thank you so much, both of you :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum