Solved

Remove non-digits from a Java String

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

808 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