Solved

Convert String to integer

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

[Webinar] Learn 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…
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

690 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