Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert String to int array in java

I have one string:

String arr = "[1,2]";

ie "[1,2]" is like a single String.

How do I convert this arr to int array in java?

like image 946
Deepak Kumar Avatar asked Oct 04 '11 10:10

Deepak Kumar


People also ask

Can you convert a string to an array?

We can also convert String to String array by using the toArray() method of the List class. It takes a list of type String as the input and converts each entity into a string array.

Can you convert strings to ints?

In Python an strings can be converted into a integer using the built-in int() function. The int() function takes in any python data type and converts it into a integer.


6 Answers

String arr = "[1,2]"; String[] items = arr.replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("\\s", "").split(",");  int[] results = new int[items.length];  for (int i = 0; i < items.length; i++) {     try {         results[i] = Integer.parseInt(items[i]);     } catch (NumberFormatException nfe) {         //NOTE: write something here if you need to recover from formatting errors     }; } 
like image 113
Saul Avatar answered Sep 20 '22 13:09

Saul


Using Java 8's stream library, we can make this a one-liner (albeit a long line):

String str = "[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]";
int[] arr = Arrays.stream(str.substring(1, str.length()-1).split(","))
    .map(String::trim).mapToInt(Integer::parseInt).toArray();
System.out.println(Arrays.toString(arr));

substring removes the brackets, split separates the array elements, trim removes any whitespace around the number, parseInt parses each number, and we dump the result in an array. I've included trim to make this the inverse of Arrays.toString(int[]), but this will also parse strings without whitespace, as in the question. If you only needed to parse strings from Arrays.toString, you could omit trim and use split(", ") (note the space).

like image 28
Jeffrey Bosboom Avatar answered Sep 21 '22 13:09

Jeffrey Bosboom


    final String[] strings = {"1", "2"};
    final int[] ints = new int[strings.length];
    for (int i=0; i < strings.length; i++) {
        ints[i] = Integer.parseInt(strings[i]);
    }
like image 25
Martin Gamulin Avatar answered Sep 20 '22 13:09

Martin Gamulin


Saul's answer can be better implemented splitting the string like this:

string = string.replaceAll("[\\p{Z}\\s]+", "");
String[] array = string.substring(1, string.length() - 1).split(",");
like image 34
Triqui Avatar answered Sep 19 '22 13:09

Triqui


It looks like JSON - it might be overkill, depending on the situation, but you could consider using a JSON library (e.g. http://json.org/java/) to parse it:

    String arr = "[1,2]";

    JSONArray jsonArray = (JSONArray) new JSONObject(new JSONTokener("{data:"+arr+"}")).get("data");

    int[] outArr = new int[jsonArray.length()]; 

    for(int i=0; i<jsonArray.length(); i++) {
        outArr[i] = jsonArray.getInt(i);
    }
like image 26
mseebach Avatar answered Sep 18 '22 13:09

mseebach


try this one, it might be helpful for you

String arr= "[1,2]";
int[] arr=Stream.of(str.replaceAll("[\\[\\]\\, ]", "").split("")).mapToInt(Integer::parseInt).toArray();
like image 27
Deepesh Kumar Avatar answered Sep 18 '22 13:09

Deepesh Kumar