Turns a string into an integer array. - Android java.lang

Android examples for java.lang:String Parse

Description

Turns a string into an integer array.

Demo Code


//package com.java2s;

public class Main {
    /**//w ww  . j a v a2s  .  c  o m
     * Turns a string into an integer array.
     * 'string' must only contain numerals separated by commas with no spaces.
     * @param string
     * @return an int array
     */
    public static int[] stringToIntArray(String string) {
        String strArray[] = string.split(",");
        int length = strArray.length;
        int intArray[] = new int[length];
        int i = 0;
        for (String str : strArray) {
            intArray[i] = Integer.parseInt(str);
            i++;
        }
        return intArray;
    }
}

Related Tutorials