| Author |
Check if a String has alphabets
|
Nikhil Das Nomula
Greenhorn
Joined: Jun 10, 2011
Posts: 23
|
|
|
I have a string which contains numbers and characters or just numbers alone or characters alone and I want to check if it contains alphabets. I think we have to use regular expressions but I am not sure how do I go about with it.
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9956
|
|
|
What have you tried?
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Eoin Brosnan
Ranch Hand
Joined: Dec 12, 2009
Posts: 37
|
|
Nikhil Das Nomula wrote:I have a string which contains numbers and characters or just numbers alone or characters alone and I want to check if it contains alphabets. I think we have to use regular expressions but I am not sure how do I go about with it.
I am not clear on what pattern you want to match but the above is a starting point. [a-zA-Z] will match any letter, upper or lower.
|
 |
Nikhil Das Nomula
Greenhorn
Joined: Jun 10, 2011
Posts: 23
|
|
@Fred rosenberger : I have tried iterating through each character but I wanted to use regular expressions.
@Eoin Brosnan: The characters can be at any place, they can be at the starting point, at the ending point or occur in the middle. Something like 989BR09 or 3988BR or SA3432DAS
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
And did you try that code with your input examples?
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Nikhil Das Nomula
Greenhorn
Joined: Jun 10, 2011
Posts: 23
|
|
I tried that code but if we pass a string which has only number then it fails.
Pattern p = Pattern.compile("[a-zA-Z]");
Matcher m = p.matcher("3453443534534");
if(m.find())
System.out.println("contains letters");
Output:
contains letters
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9956
|
|
I am not a regex expert, but you may need a '+' on your pattern to say "one or more"
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Nikhil Das Nomula wrote:Output:
contains letters
Odd. I just copy-pasted that code and get no output at all, as expected.
|
 |
Darryl Burke
Bartender
Joined: May 03, 2008
Posts: 4167
|
|
fred rosenberger wrote:I am not a regex expert, but you may need a '+' on your pattern to say "one or more"
Not for find(). You would for matches(), if you need to test that the whole String consists of characters [a-zA-Z]. To ascertain the presence of at least one alpha character using matches(), this will suffice.
|
luck, db
There are no new questions, but there may be new answers.
|
 |
Nikhil Das Nomula
Greenhorn
Joined: Jun 10, 2011
Posts: 23
|
|
@Rob Spoor: Here is the code that I tested with
public class RegularExpressionTest {
public static void main(String args[]){
Pattern p = Pattern.compile("[a-zA-Z]");
Matcher m = p.matcher("3453443VD534534");
if(m.find())
System.out.println("contains letters");
else{
System.out.println("no letters");
}
}
}
|
 |
Nikhil Das Nomula
Greenhorn
Joined: Jun 10, 2011
Posts: 23
|
|
|
@Darryl Burke I tried doing that and it works !! Thanks a lot !
|
 |
Nikhil Das Nomula
Greenhorn
Joined: Jun 10, 2011
Posts: 23
|
|
Just FYI here is the code that actually worked. Thanks to Darryl Burke
|
 |
Aditya Jha
Ranch Hand
Joined: Aug 25, 2003
Posts: 227
|
|
The .* thing is not strictly needed when using find() (as opposed to match()).
This code works just fine:
|
 |
 |
|
|
subject: Check if a String has alphabets
|
|
|