If result
is an array of char
, try this:
public static char[] split(String str, int numChar) {
char[] s = str.toCharArray();
if (numChar >= str.length()) {
return s;
}
char[] r = new char[numChar];
System.arraycopy(s, 0, r, 0, numChar);
return r;
}
Or if result
is a String
, try this:
public static String split(String str, int numChar) {
char[] s = str.toCharArray();
if (numChar >= str.length()) {
return String.copyValueOf(s);
}
char[] r = new char[numChar];
System.arraycopy(s, 0, r, 0, numChar);
return String.copyValueOf(r);
}
Please note that two of above methods do not change the original String.