aspose file tools*
The moose likes Java Micro Edition and the fly likes Remove Special Characters from String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Java Micro Edition
Bookmark "Remove Special Characters from String" Watch "Remove Special Characters from String" New topic
Author

Remove Special Characters from String

Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
I've got a telephone number in a String like so:

(123) 456-7890

What would be the easiest way to remove all special characters, including spaces, and just display the numbers?

I need the string to equal "1234567890".

Thanks!

Drew
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30537
    
150

Drew,
The easiest thing I can think of is to use String's replaceAll method. You could use a regular expression to match all characters except digits and replace them with an empty string.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Drew Lane
Ranch Hand

Joined: May 13, 2001
Posts: 296
Thanks for the reply.

I'm doing mobile so I don't have the luxury of replaceAll. :-)

Right now I'm converting the string to a char[] and looping through each char.

Seems really awkward though.

Figured I should ask before I reinvent the wheel. :-p

Drew
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

Since several classes and methods are missing for you, I'm afraid you have little choice.

You can use either a StringBuffer for adding, or create a new array of the same size, copy only the characters you need and then use the String(char[], int, int) constructor.
[ September 30, 2008: Message edited by: Rob Prime ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
Originally posted by Drew Lane:
Thanks for the reply.

I'm doing mobile so I don't have the luxury of replaceAll. :-)

Right now I'm converting the string to a char[] and looping through each char.

Seems really awkward though.

Figured I should ask before I reinvent the wheel. :-p

Drew


Yes, that's pretty much how to do it if you can't use String's replaceAll(...) method(s).
You could write a convenience method:


which will do the trick if your Strings don't get large (for large Strings, it may become a time consuming task).
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

Originally posted by Piet Verdriet:

A small improvement if you expect only few "foreign" characters:

Caching text.length() would be a good idea then as well

which will do the trick if your Strings don't get large (for large Strings, it may become a time consuming task).

But I don' think the performance would be a lot better using the JavaSE libraries. In the end, every character will need to be checked anyway.
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
Both the caching and providing an int as parameter to the StringBuffer will improve next to nothing if the strings are large and my code is used. Most of the time, the cpu is busy with the loops.
But when improving the code I posted, it may be worth while to do this.

Originally posted by Rob Prime:

But I don' think the performance would be a lot better using the JavaSE libraries. In the end, every character will need to be checked anyway.


If 'text' and 'charsToKeep' from:


are large, then it's worth it to make 'charsToKeep' into a Set<Character> making the algorithm linear instead of quadratic.
[ September 30, 2008: Message edited by: Piet Verdriet ]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

Except you don't have Set in JavaME
Vector and Hashtable is as far as you get as far as the Collection framework is concerned. Using Hashtable in combination with containsKey and using the key as the values as well can help you out, but you get that darned synchronization (if that exists in JavaME).

Also, the length of text doesn't matter since you have to iterate over that anyway.


Another performance improvement, if you only need this for digits:

That removes the indexOf lookups.
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
Originally posted by Rob Prime:
Except you don't have Set in JavaME


D@mn!
It looks like J2ME programming is like crossing the ocean in a row boat!

[ September 30, 2008: Message edited by: Piet Verdriet ]
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16065
    
  21

Wimps. You're all spoiled. Back when I worked with C and the standard libraries consisted of things like stdio.h and I was working on a different platform every month, it seemed like I was reinventing this stuff almost daily.

Which is one reason I prefer Java to C.

Here's one that may be a tad faster, possibly (no guarantees) more compact. It trades flexibility for speed.



Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Remove Special Characters from String