Solved

Remove non-digits from a Java String

Posted on 2000-03-30
5
833 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

785 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