AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

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

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
Matt
Guest





PostPosted: Sat Aug 21, 2004 3:13 pm    Post subject: test a string has only alphabets [a-zA-Z] Reply with quote



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)
Back to top
Paul Lutus
Guest





PostPosted: Sat Aug 21, 2004 3:20 pm    Post subject: Re: test a string has only alphabets [a-zA-Z] Reply with quote



Matt wrote:

Quote:
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.

Yes, perhaps, although there are several other approaches. Is this a part of
your Java lesson? If so, why not try creating some code and post it, asking
questions about a specific code example?

I ask this because you don't learn how to write computer programs by asking
other people to code for you.

--
Paul Lutus
http://www.arachnoid.com


Back to top
zoopy
Guest





PostPosted: Sat Aug 21, 2004 3:49 pm    Post subject: Re: test a string has only alphabets [a-zA-Z] Reply with quote



On 21-8-2004 17:13, Matt wrote:

Quote:
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)

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

Then you'll have to create an appropriate reg expr, for which there are several posibilities
(p{Alpha} could be part of it).

--
Regards,
Z.

Back to top
Nick Pomfret
Guest





PostPosted: Sat Aug 21, 2004 4:04 pm    Post subject: Re: test a string has only alphabets [a-zA-Z] Reply with quote

the String.matches is what you need. it takes one paramter which is a
regular expression - use "[a-zA-Z]+" to match all characters in the
alphabet. here is a test case to illustrate:

import java.io.IOException;
import junit.framework.TestCase;

public class Test extends TestCase {
public void test() {

assertTrue("abcXZY".matches("[a-zA-Z]+"));
assertFalse("abc3XZY".matches("[a-zA-Z]+"));

}
}


--

* --------------------------------------------------*

For feature-rich JTables visit
http://www.tabletoolkit.com

"Matt" <jrefactors (AT) hotmail (DOT) com> wrote

Quote:
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)



Back to top
Tony Morris
Guest





PostPosted: Sun Aug 22, 2004 3:30 am    Post subject: Re: test a string has only alphabets [a-zA-Z] Reply with quote

public static boolean isAlpha(String s)
{
for(int i = 0; i < s.length(); i++)
{
if(!Character.isAlpha(s.charAt(i)))
{
return false;
}
}

return true;
}

--
Tony Morris
http://xdweb.net/~dibblego/


Back to top
zoopy
Guest





PostPosted: Sun Aug 22, 2004 6:49 am    Post subject: Re: test a string has only alphabets [a-zA-Z] Reply with quote

On 22-8-2004 5:30, Tony Morris wrote:
Quote:
public static boolean isAlpha(String s)
{
for(int i = 0; i < s.length(); i++)
{
if(!Character.isAlpha(s.charAt(i)))
{
return false;
}
}

return true;
}

Test.java:12: cannot resolve symbol

symbol : method isAlpha (char)
location: class java.lang.Character
if(!Character.isAlpha(s.charAt(i)))
^
1 error
--
Regards,
Z.

Back to top
Tony Morris
Guest





PostPosted: Sun Aug 22, 2004 7:20 am    Post subject: Re: test a string has only alphabets [a-zA-Z] Reply with quote

sorry, "isLetter"

public static boolean isLetter(String s)
{
for(int i = 0; i < s.length(); i++)
{
if(!Character.isLetter(s.charAt(i)))
{
return false;
}
}

return true;
}


--
Tony Morris
http://xdweb.net/~dibblego/


"zoopy"
Quote:
On 22-8-2004 5:30, Tony Morris wrote:
public static boolean isAlpha(String s)
{
for(int i = 0; i < s.length(); i++)
{
if(!Character.isAlpha(s.charAt(i)))
{
return false;
}
}

return true;
}

Test.java:12: cannot resolve symbol
symbol : method isAlpha (char)
location: class java.lang.Character
if(!Character.isAlpha(s.charAt(i)))
^
1 error
--
Regards,
Z.



Back to top
Thomas Schodt
Guest





PostPosted: Sun Aug 22, 2004 7:26 am    Post subject: Re: test a string has only alphabets [a-zA-Z] Reply with quote

zoopy wrote:

Quote:
Test.java:12: cannot resolve symbol
symbol : method isAlpha (char)
location: class java.lang.Character
if(!Character.isAlpha(s.charAt(i)))
^

Left as an exercise for OP ?

OP should really read the javadoc on Character.is*() functions;
what is considered "alpha" characters (aka. letters) are
[A-Za-z] and a handful of non-ASCII characters.

Back to top
anon
Guest





PostPosted: Mon Aug 23, 2004 3:48 pm    Post subject: Re: test a string has only alphabets [a-zA-Z] Reply with quote

Tony Morris wrote:
Quote:
sorry, "isLetter"

public static boolean isLetter(String s)
{
for(int i = 0; i < s.length(); i++)
{
if(!Character.isLetter(s.charAt(i)))
{
return false;
}
}

return true;
}



Just to throw my hat in the ring on this debate...

The above code is a better general-purpose solution than the new
String.matches(regex) method because even now, not everyone is using JSE
1.4.x In my shop (large US bank) we're still on 1.3.x and will be for
some time.

Back to top
anon
Guest





PostPosted: Mon Aug 23, 2004 6:38 pm    Post subject: Re: test a string has only alphabets [a-zA-Z] Reply with quote

Tony Morris wrote:
Quote:
sorry, "isLetter"

public static boolean isLetter(String s)
{
for(int i = 0; i < s.length(); i++)
{
if(!Character.isLetter(s.charAt(i)))
{
return false;
}
}

return true;
}


Also, the above code will return true if s.length()==0. Probably not

the desired result.

A better version IMO, which will match the result of
s.matches("[a-zA-Z]+") is:

public static boolean isLetter(String s)
{
final int n = s.length();

for (int i = 0; i < n; i++)
if (!Character.isLetter(s.charAt(i)))
return false;

return (n > 0);
}

Back to top
John Fereira
Guest





PostPosted: Thu Aug 26, 2004 7:28 pm    Post subject: Re: test a string has only alphabets [a-zA-Z] Reply with quote

Thomas Schodt <spamtrap (AT) xenoc (DOT) demon.co.uk.invalid> wrote in news:cg9hqe$gh6
$1$8302bc10 (AT) news (DOT) demon.co.uk:

Quote:
zoopy wrote:

Test.java:12: cannot resolve symbol
symbol : method isAlpha (char)
location: class java.lang.Character
if(!Character.isAlpha(s.charAt(i)))
^

Left as an exercise for OP ?

OP should really read the javadoc on Character.is*() functions;
what is considered "alpha" characters (aka. letters) are
[A-Za-z] and a handful of non-ASCII characters.

In which case, a solution may be found using String.getBytes() and testing
elements of the array against a range of decimal values.




Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.