• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Remove non-letter characters from String

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> I know that there is a method String.replaceAll

take a peek at the arguments for that method.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right. It IS regex. Thanks for reminds me...
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic