Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Remove non-digits from a Java String

Posted on 2000-03-30
5
Medium Priority
?
846 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 150 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

705 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