0

What is the fastest way to convert string to string array like below(split by 2 char):
String input="127856";
String[] output={"12" , "78" , "56"};
Currently I'm using for but very slow:

public static String[] mySplit(String input){
    List<String> res=new ArrayList<String>();
    for (int i = 0; i < input.length()-1; i+=2) {
        res.add("" + input.charAt(i) + "" + input.charAt(i+1));
    }
    String array[]=res.toArray(new String[0]);
    return array;
}
Micle
  • 159
  • 10
  • Here's a one liner: `String[] arr = s.split("(?<=\\G..)");` – GBlodgett May 16 '19 at 19:40
  • @GBlodgett I doubt a regex would be faster than a loop, though – Eugene May 16 '19 at 19:41
  • 1
    In which way do you consider your code to be too slow? What kind of measurements lead to that insight "it's too slow"? – GhostCat May 16 '19 at 19:42
  • 1
    that is slow (concatenationo of chars ans strings) `"" + input.charAt(i) + "" + input.charAt(i+1)` - try `input.substring(i, i+2)` (also creating a list to create an array is not the fastest.. despite easiest) – user85421 May 16 '19 at 19:44

4 Answers4

1

If you don't want to use regex, you can use substring, which should be much quicker than string concatenation:

public static String[] mySplit(String input){
    int len = input.length(), index = 0;

    // Arrays are faster than lists
    String[] array = new String[len/2+len%2];
    for (int i = 0; i < len-1; i+=2, index++) {
        array[index]=input.substring(i,i+2);
    }
    // To handle strings with an odd number of characters
    if(input.length()%2==1) {
        array[index]=input.substring(input.length()-1);
    }
    return array;
}

Demo

Benjamin Urquhart
  • 1,626
  • 12
  • 18
1

The output array of your function should contain half of the input length elements (e.g. out.length is input.length() / 2). Next, each position in the output starts at the output index multiplied by 2 and ends at one more than that (but String.substring(int, int) is exclusive). I think the "fastest" way to write your function is something like,

public static String[] mySplit(String input) {
    String[] out = new String[(1 + input.length()) / 2];
    for (int i = 0; i < out.length; i++) {
        out[i] = input.substring(i * 2, Math.min(2 + (i * 2), input.length()));
    }
    return out;
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

You can also use regex for it:

public static String[] mySplit(String input){

  String[] output = input.split("(?<=\\G.{2})");
  return output;
}

The regex (?<=\G...) matches an empty string that has the last match (\G) followed by three characters (...) before it ((?<= )).

However, The Time complexity would be the same.

Rishabh Agarwal
  • 1,988
  • 1
  • 16
  • 33
0

It is often the best solution to use the standard libraries. java.lang.String provides a method String[] split(String regex). In a regex, a . stands for a single char, you want 2 chars, so the regex is ..

public static String[] mySplit(String input){
   String array[]=input.split("..");
   return array;
}

As it is untested, i cannot tell you what happens if the numbers of chars in the string is odd.

Edit: As it happens with untested code, the regex is wrong. Please disregard my answer.