Skip to content

Instantly share code, notes, and snippets.

@ferronrsmith
Created July 21, 2014 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ferronrsmith/22c15e743acea1c08272 to your computer and use it in GitHub Desktop.
Save ferronrsmith/22c15e743acea1c08272 to your computer and use it in GitHub Desktop.
Delimited Split
private String split(String pInputString, String pDelim, int pMaxLength) {
int len = pInputString.length();
int times = len / pMaxLength;
boolean remainder = len % pMaxLength != 0;
int startIdx = 0;
int endIdx = pMaxLength;
StringBuilder res = new StringBuilder();
if (len <= pMaxLength) {
return pInputString;
} else {
for (int i = 0; i < times; i++) {
res.append(pInputString.substring(startIdx, endIdx));
res.append(pDelim);
// increment
endIdx += pMaxLength;
startIdx += pMaxLength;
}
if (remainder) {
res.append(pInputString.substring(startIdx));
}
}
return res.toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment