Solved

Convert String to integer

Posted on 2002-04-02
2
432 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
[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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
tomcat startup error 5 136
Android development question 2 81
jsp CRUD operations with and without prepared statement also hibernatge 1 31
JDBC performance 8 29
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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:

730 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