Solved

Remove non-digits from a Java String

Posted on 2000-03-30
5
829 Views
Last Modified: 2008-03-10
What's the most efficient way to remove non-digit characters from a String in Java? In Perl it would be the one liner
$string =~ s/\D//g;
(This is one of the reasons I like Perl for web-based stuff)

Anyway, most of the solutions I can think of involve iteration, and constant building of Strings, and I'd prefer not to get down to the StringBuffer level, unless it was really clear how to do so.
0
Comment
Question by:kisrael
  • 2
  • 2
5 Comments
 
LVL 1

Expert Comment

by:jjdunne
ID: 2669740
Here is one possible solution.  It is not as pretty as Perl, but it works.

String s = "123ffd455";
StringBuffer sb = new StringBuffer();

for (int i=0; i<s.length(); i++) {
    if (Character.isDigit(s.charAt(i)))
        sb.append(s.charAt(i));
}

String sResult = sb.toString();
0
 
LVL 1

Accepted Solution

by:
jjdunne earned 50 total points
ID: 2669756
Here is one possible solution.  It is not as pretty as Perl, but it works.

String s = "123ffd455";
StringBuffer sb = new StringBuffer();

for (int i=0; i<s.length(); i++) {
    if (Character.isDigit(s.charAt(i)))
        sb.append(s.charAt(i));
}

String sResult = sb.toString();
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2669904
If you really like Perl regular expressions, take a look at the PerlTools package at http://www.savarese.org/oro/downloads/. It should reduce this task to a few lines of Java code using perl5 pattern syntax.

This package is free. Distribution terms are essentially that you can use the
product without fee to write new software, but you can't distribute
it as an independent software unit without first negotiating
a licensing agreement. In other words, you can't bundle
PerlTools with a set of development tools.

There are other Java regular expression packages available (including gnuregexp from the Free Software Foundation) but this one (actually OROMatcher on which it is based) seems to be regarded as the best.

Best regards,
Jim Cakalic
0
 

Author Comment

by:kisrael
ID: 2680692
Thanks for the pointer Jim.  it's a bit more than I have time to deal with, but I'll save that bookmark.

Huh- I thought I 'accept as answer' one of the previous things, but it didn't seem to work.  Maybe the double post is confusing things?
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 2757506
It's been awhile and the duplicate post appears to have been removed. Why don't you try to accept your answer again.

Jim
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now