English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have a string of integers, such as the following:

"12 24 12 9 2"

and I want to convert this into an array which will
seperate the string into an array of integers.

How do I do this

please be as specific as possible

2007-03-19 17:21:14 · 5 answers · asked by π∑∞∫questionqueen 3 in Computers & Internet Programming & Design

sorry i forgot to put the language
JAVA

2007-03-19 17:43:02 · update #1

5 answers

In Java,

java.util.StringTokenizer token = new ...(input);
int[] array = new int[token.getSize()];
int index = 0;
while (token.hasNext()) array[index++] = Integer.parse(token.next());

2007-03-19 18:26:12 · answer #1 · answered by Andy T 7 · 0 0

tokenize your string.

in your example, you have a space-delimited string, so tokenize the string by spaces. this means split the string:

"12 24 12 9 2"

into the strings:

"12"
"24"
"12"
"9"
"2"

this capability is built into several languages, so search your api for token or tokenize. now you have an array of strings, stripped of delimiters, each string representing an integer value. search your api again for something like toInt or parseInt or intVal to convert the string to an integer. again, this is often an existing function in high-order languages.

if you care to name your language, i can show you code.

2007-03-20 00:36:41 · answer #2 · answered by T. Bogard 1 · 0 0

You dont state the language you are using for coding the answer.

Check the SPLIT method

SPLIT takes a string of any length that is formed by data separated by any separator (let's say a comma or space)

You only need to state the string to 'separate' and the separator, the output will be an array.

2007-03-20 00:34:14 · answer #3 · answered by Cat 9 6 · 0 0

It depends on the programming language you use. for example, in perl, u can just use 'split' but if you need to do in something like C, you need to write the whole code or use strtok() or strtok_r(). In case of strtok() or strtok_r() after tokenizing you need to convert strings to integer which u can do it urself or use sscanf.

2007-03-20 00:39:23 · answer #4 · answered by Kutti Jr 2 · 0 0

use string tokenizer

2007-03-20 03:21:26 · answer #5 · answered by Anonymous · 0 0

fedest.com, questions and answers