test a string has only alphabets [a-zA-Z]

M

Matt

If I want to test a string has only alphabets [a-zA-Z], there is no
String API to do that.
Is that true? I guess the best approach is to use reg expression.
Please advise. thanks!

boolean testAlphaString(String s)
 
T

Thomas Kellerer

Matt wrote on 21.08.2004 17:13:
If I want to test a string has only alphabets [a-zA-Z], there is no
String API to do that.
Is that true? I guess the best approach is to use reg expression.
Please advise. thanks!

boolean testAlphaString(String s)

A quick look into the Javadoc of String reveals the following method:

<http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#matches(java.lang.String)>

public boolean testAlphaString(String s)
{
return s.matches("[a-zA-Z]");
}

This can be tuned by pre-compling the pattern using Pattern.compile()

Thomas
 
P

Paul Lutus

Matt said:
If I want to test a string has only alphabets [a-zA-Z], there is no
String API to do that.
Is that true? I guess the best approach is to use reg expression.
Please advise. thanks!

Here's my advice. Don't multi-post.
 
T

Thomas Schodt

Thomas said:
public boolean testAlphaString(String s)
{
return s.matches("[a-zA-Z]");
}

You left a bug in there as an exercise for the OP ?
I would not imagine OP is familiar with regex syntax,
so a hint might be in order:

The above matches only
strings that consist of exactly 1 alpha character (us-ascii letter).
 
T

Thomas Kellerer

Thomas Schodt wrote on 21.08.2004 17:41:
Thomas said:
public boolean testAlphaString(String s)
{
return s.matches("[a-zA-Z]");
}

You left a bug in there as an exercise for the OP ?
I would not imagine OP is familiar with regex syntax,
so a hint might be in order:

The above matches only
strings that consist of exactly 1 alpha character (us-ascii letter).

Of course you are right :)

I did not intend to.
I simply copied the example of the OP into the regex (as he already gave an
example of the "desired" regex)

Thomas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top