File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Efficient Way To Remove Non Alphanumerics From A String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Efficient Way To Remove Non Alphanumerics From A String" Watch "Efficient Way To Remove Non Alphanumerics From A String" New topic
Author

Efficient Way To Remove Non Alphanumerics From A String

Dave Hewy
Ranch Hand

Joined: Aug 21, 2003
Posts: 93
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

Joined: Sep 18, 2003
Posts: 14
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.


<a href="http://www.netspade.com/" target="_blank" rel="nofollow">http://www.netspade.com/</a> - Web programming articles and tutorials.
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
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


42
Praveen Balaji
Ranch Hand

Joined: Jun 17, 2001
Posts: 60
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

Joined: Aug 21, 2003
Posts: 93
Thanks for all your replies - most helpful
Cheers
Dave
Jamie Robertson
Ranch Hand

Joined: Jul 09, 2001
Posts: 1879

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Efficient Way To Remove Non Alphanumerics From A String