|
Jul
30
|
The following code will split the string into chunks, each of length ‘nchars’ (except potentially the last chunk)
int len = string.length();
for (int i=0; i<len; i+=nchars)
{
String part = string.substring(i, Math.min(len, i + nchars)));
}
Another more exotic option uses regular expression and the spit() method. The number of dots in the regexp controls the length of each part.
// Splits string into 3 character long pieces.
String[] parts = string.split("(?<=\\G...)");
See how to fill a string with a character for how to dynamically generate a regexp for any number of characters. Though I’d probably go with the first method.



December 30th, 2010 at 2:32 am
Thanks.
This could be really useful.
:3
Breisa