Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

How can we check if a string contains only alphabets?

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
I desperately need some help on how we can check if a string contains only alphabets in JAVA?
I tried some of the programs on the net which uses reg expressions but they just don seem to work or I am not sure if I am doing a mistake.
As per one of the forums that suggested a solution like the code given below:

public class AlphabetCheck {
public static void main(String args[]) {
String myString = "hellothisisastring";
if(myString.matches("[a-zA-Z]")){
System.out.println("string has only alphabets");
}
else {
System.out.println("It has other characters");
}
}
}

I checked this in Eclipse and though the string has only characters if always returns the statement in the else block.

I dont want to do the check using the "Character" class as it is very tedious and I want a one shot return statement than a return when every character is checked.

Please can someone help me with this??? I would be very very grateful.

Thanks in Advance

Cheers
Anjali
 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For the regular expressions solution..

Check the Pattern class documentation
The Sun reg ex tutorial
An online reg ex tester..

[a-zA-Z] matches a single character of this specified class (i.e. lowercase or uppercase alphabetic character).

Now say you want to match one or more alphabetic characters.
You need to use something called a quantifier. Use the first link, find the relevant quantifier and suffix the [a-zA-Z].
Like [a-zA-Z]<quantifier goes here>.

Tinker with it and find your solution.. you could match an exact number of characters.. match a minimum or maximum number etc... it's all there for you to try..



 
Greenhorn
Posts: 3
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:

String myString = "hellothisisastring";
if(Pattern.matches("[a-zA-Z]+", myString))
{
System.out.println("Yep!");
}
else
{
System.out.println("Nope!");
}
 
Anjali Raman
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for the help
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic