Finding an integer in a string

I'm looking for suggestions on the best way to find a number in a string, pull it out and convert it into an integer.

I know that the string class has a lot of available methods, but none seem to be what I need. I thought about using the getChar method

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#getChars(int,%20int,%20char[],%20int)

making the string into an array of characters and then searching for integer in it, but that seems to be a very sloppy and slow process. Is there another way I can go about this? I can't use the strings contains method because I will no know what the number will be. I also don't know the length of the string, so I cannot use any of the methods stating to look at a certain index of the string.

Thanks for any help.

[804 byte] By [Slackware_usera] at [2007-11-26 20:20:49]
# 1

Example in- and output?

For example, what would be your output of the following Strings?

String a = "abc3def";// 1 ordinary integer

String b = "abc33de88f";// > 1 ordinary integer

String c = "abc3.1def"; // floating point

String d = "abc-5def"; // negative value

String e = "abc9223372036854775808def";// > Long.MAX_VALUE

prometheuzza at 2007-7-10 0:45:40 > top of Java-index,Java Essentials,Java Programming...
# 2
And what do you mean by "slow process"?
prometheuzza at 2007-7-10 0:45:40 > top of Java-index,Java Essentials,Java Programming...
# 3

> methods, but none seem to be what I need. I thought

> about using the getChar method

> http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Stri

> ng.html#getChars(int,%20int,%20char[],%20int)

> making the string into an array of characters and

> then searching for integer in it, but that seems to

> be a very sloppy and slow process.

Why do you think it would be slow? What other way for finding a number inside a string could there be besides stepping through each character? Even if a "findNumberInString" method existed in the String class, it would have to do that.

(Note that you don't need to copy the chars to a new array--you could just use charAt. Multple method invocations vs. an array copy, it's probably a horse apiece performance-wise.)

> Is there another

> way I can go about this? I can't use the strings

> contains method because I will no know what the

> number will be. I also don't know the length of the

> string,

If you have a String, you always know its length.

You can create a loop that looks at each character of the string, or you could use regex (the spilt method). I'd go for the latter, but if you're not familiar with it and ren't able to take the time to learn it now, then I'd advise against it.

[url=http://java.sun.com/docs/books/tutorial/extra/regex/index.html]Sun's Regular Expression Tutorial for Java[/url]

[url=http://www.regular-expressions.info/]Regular-Expressions.info[/url]

jverda at 2007-7-10 0:45:40 > top of Java-index,Java Essentials,Java Programming...
# 4

Hi,

Try this code--

/**

*

*/

package com.test;

/**

* @author alok

*

*/

public class StringRegx

{

public static void main(String[] args)

{

String context="This 98 is a sample 99message 76 85";

String[] tokens;

tokens=context.split("[^0-9]");

int i=0;

for(i=0;i<tokens.length;i++){

if(!(tokens.equals("")))

System.out.println(tokens );

}

}

}>

AlokSharmaa at 2007-7-10 0:45:40 > top of Java-index,Java Essentials,Java Programming...
# 5

> Hi,

> Try this code--

>

> /**

> *

> */

> package com.test;

> ...

What about this String:String context="Pi is approximately 3.14159265358979323846264338327950288419716939937510582097494459. Which does not equal -1, of course.";

prometheuzza at 2007-7-10 0:45:40 > top of Java-index,Java Essentials,Java Programming...