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

Efficient Way To Remove Non Alphanumerics From A String

 
Dave Hewy
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the most efficient way to strip out non alphanumeric characters from a string assuming it's uppercase and I only want the first 10 non alphanumerics?
And I can't use regular expressions !
At the moment I have converted it to a char array, then considered each element before putting that into a new char array, but is there a more elegant solution?
Thanks
Dave
 
Jason Davies
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That method seems pretty efficient to me. I don't think there are any methods in the standard API for doing it, so there's not much chance of an "elegant" solution in that sense.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In 1.4 you can use regular expressions to filter the string.
I'm no wizard with them, so someone else will have to come up with the actual expression to use
 
Praveen Balaji
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I dont know much about Regular expressions, but if you requirement is specific (removing non-alphanumeric), I suggest a custom code, because nothing can be more efficient than a specialized code.

[ September 18, 2003: Message edited by: praveen balaji ]
 
Dave Hewy
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all your replies - most helpful
Cheers
Dave
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at something like this:

\D means a non-digit in regex land. Most of the time, the JDK option is fast enough. Less coding and maintenance for yourself!
Jamie
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic