Solved

Remove non-digits from a Java String

Posted on 2000-03-30
5
841 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

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 will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

628 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