此处采用这种方法。 我想知道,我是否在违反这方面的任何最佳做法,或者在语言方面,我是否做过错。
private List<String> breakStringInChunks(String text, int chunkSize) {
List<String> chunks = new ArrayList<String>();
String temporary = "";
int numberOfChunks = text.length() / chunkSize;
int beginIndex = 0;
int endIndex = 0;
// Add one iteration if numberOfChunks*chunkSize is less than the length of text.
if ((numberOfChunks * chunkSize) < text.length()) {
numberOfChunks++;
}
// Cut strings and add in the list.
for (int i = 0; i < numberOfChunks; i++) {
endIndex+=chunkSize;
if ((i + 1) == numberOfChunks) {
temporary = text.substring(beginIndex);
}
else {
temporary = text.substring(beginIndex, endIndex);
}
beginIndex=endIndex;
chunks.add(temporary);
}
return chunks;
}