File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Remove non-letter characters from String

 
Timotius Pamungkas
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I know that there is a method String.replaceAll

take a peek at the arguments for that method.
 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Timotius Pamungkas
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right. It IS regex. Thanks for reminds me...
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Justin Fox
Ranch Hand
Posts: 802
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic