Solved

Convert String to integer

Posted on 2002-04-02
2
429 Views
Last Modified: 2010-03-31
have a String.. something like "abc123", need to convert it into another String which contains only numeric characters. ie. the result should be "123"
0
Comment
Question by:whdev1
  • 2
2 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 6913795
StringBuffer sb = new StringBuffer("abc123");

for (int i=0;i<sb.length();i++) {
   if (sb.charAt(i) < 30 || sb.charAt(i) > 39) {
       sb = sb.deleteCharAt(i);
   }
}

System.out.println(sb.toString());


0
 
LVL 18

Accepted Solution

by:
bobbit31 earned 50 total points
ID: 6913822
actually, try this (i tested this one ;))

String s = new String("abc1234567890");
String strReturn = "";

for (int i=0;i<s.length();i++) {
  if ((int) s.charAt(i) >= 48 && (int) s.charAt(i) <= 57) {
      strReturn += s.charAt(i);
  }
}

System.out.println(strReturn);
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
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:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

948 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

22 Experts available now in Live!

Get 1:1 Help Now