File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Removing Alpha Characters from a String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Removing Alpha Characters from a String" Watch "Removing Alpha Characters from a String" New topic

Removing Alpha Characters from a String

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1562
I need the best practice for striping alpha characters a string. Example: 849650-7BLANK

I have this but it will not work

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

That first argument is supposed to be a regular expression. A well-formed regular expression representing a sequence of one or more alphabetic characters would be "\{L}+" (this matches all Unicode letters.) With the proper escaping, this looks like

pnstring = pnstring.replaceAll("\\{L}+","");

[Jess in Action][AskingGoodQuestions]
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262
Actually, it's "\\p{L}", but with single-letter category names you can drop the braces:
[ July 21, 2008: Message edited by: Alan Moore ]
I agree. Here's the link:
subject: Removing Alpha Characters from a String
It's not a secret anymore!