File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Remove non-letter characters from String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Remove non-letter characters from String" Watch "Remove non-letter characters from String" New topic
Author

Remove non-letter characters from String

Timotius Pamungkas
Greenhorn

Joined: Feb 26, 2005
Posts: 28
Hi all..
I'd like to ask how to remove all non-letter characters from a string.

e.g. if I have string "Great Javaranch" or "Gre745at Java{}#@$ranch "
and I pass each of them into specific method ( e.g. format(String) ), the method will returns "GreatJavaranch".

I know that there is a method String.replaceAll. However, my problem is how to check all non-letter character without cumbersome code or iteration.

I surely don't want to write something like


if (string.contains('.')) {
string = string.replaceAll('.', null);
}
if (string.contains('$')) {
string = string.replaceAll('.', null);
}
...
...

Or, if I have to, maybe it will be nice to have a list of all non-letter-recognized-by-java-characters, and iterate using that list. But I don't know how to get such list, and even if I can, it will be a cumbersome iteration.

Does regex works for this problem? If so, how?

Thanks
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I know that there is a method String.replaceAll

take a peek at the arguments for that method.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18840
    
  40

Does regex works for this problem? If so, how?


Yes... it could be done with something like this.



Unfortunately, regular expressions can't be explained with a single paragraph. I recommend that you do some research on it...

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Timotius Pamungkas
Greenhorn

Joined: Feb 26, 2005
Posts: 28
You're right. It IS regex. Thanks for reminds me...
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi all,


or you can use the static method isLetter(char c) from the Character class.


eg

In this case you'll need some iteration.



But the regex way is a bit shorter...

Yours,
Bu.
[ October 31, 2006: Message edited by: Burkhard Hassel ]

all events occur in real time
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802


You down with OOP? Yeah you know me!
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Hi ranchers,

I forgot yesterday:
Character.isLetter(char c) and the regex pattern
\\p{Alpha}

behave differently.
the regex pattern recognized ascii letters only, while the isLetter method also recognizes non-ascii letters, eg the one in Caf.


Yours,
Bu
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Remove non-letter characters from String