Android String to Int Convert stringToIntArray(String arrString, String separator)

Here you can find the source of stringToIntArray(String arrString, String separator)

Description

string To Int Array

Declaration

public static final int[] stringToIntArray(String arrString,
            String separator) 

Method Source Code

//package com.java2s;

public class Main {
    public static final int[] stringToIntArray(String arrString,
            String separator) {//from   ww w.j a v a2 s . c  o m
        try {
            String s = arrString.replaceAll("\\s", "");
            String[] sa = s.split(separator);
            int[] ia = new int[sa.length];
            for (int i = 0; i < sa.length; i++) {
                if (isDigits(sa[i])) {
                    ia[i] = Integer.parseInt(sa[i]);
                } else {
                    return null;
                }
            }
            return ia;
        } catch (NumberFormatException e) {
            return null;
        }
    }

    public static final boolean isDigits(String s) {
        if (s == null || s.length() == 0)
            return false;
        int begin = (s.charAt(0) == '-') ? 1 : 0;
        for (int i = begin; i < s.length(); i++) {
            if (!Character.isDigit(s.charAt(i)))
                return false;
        }
        return true;
    }

    public static final boolean isDigits(Object o) {
        if (o == null) {
            return false;
        }
        String s = o.toString();
        if (s.length() == 0)
            return false;
        int i = (s.charAt(0) == '-') ? 1 : 0;
        for (int j = s.length(); i < j; i++) {
            if (!Character.isDigit(s.charAt(i)))
                return false;
        }
        return true;
    }
}

Related

  1. getFirstInteger(String value)
  2. tryParse(String value, int defaultValue)
  3. tryParse(String value, String defaultValue)
  4. parseInt(String str)
  5. parseInt(String arg, int defaultValue)
  6. getIntegerArrayList(String strInput)
  7. validInt(CharSequence integer)
  8. parseIntOrZero(CharSequence charSequence)
  9. parseInt(String string, int defValue)