Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Remove Special Characters from String

 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 34422
347
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20533
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18169
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic