Tuesday, March 12, 2013

Convert a String/Text into an Integer Array in JS

In Java sometimes you have a long string of numbers or text delimited by a space or comma. This code below converts it simply into an Integer array. You can change your delimiter under split(" "); I've set it to space for default:

 String[] parts = wholef[0].split(" ");
 int[] intwholef= new int[parts.length];

 for(int n = 0; n < parts.length; n++) {
    intwholef[n] = Integer.parseInt(parts[n]);
  }

No comments:

Post a Comment