Solved

Convert String to integer

Posted on 2002-04-02
2
428 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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
commonTwo  challenge 63 98
basic hardware to learn oop advanced design patterns 3 72
mapBully challenge 6 88
topping1 challenge 7 48
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…
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

744 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

12 Experts available now in Live!

Get 1:1 Help Now