### 如何优化此方法以打破块中的字符串？

``````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;
}

return chunks;
}
``````

#### 7 个答案:

Briefer仍然，并避免可能调整结果列表的大小。

``````private static List<String> breakStringInChunks(final String text, final int chunkSize) {
final int numChunks = 0 == (text.length() % chunkSize) ? text.length() / chunkSize : 1 + (text.length() / chunkSize);
final List<String> chunks = new ArrayList<String>(numChunks);
for (int startIndex = 0; startIndex < text.length(); startIndex += chunkSize) {
final int endIndex = Math.min(text.length(), startIndex + chunkSize);
}
return chunks;
}
``````

``````private List<String> breakStringInChunks(String text, int chunkSize) {
int nChunks = (int)Math.ceil(((double)text.length())/chunkSize));
List<String> chunks = new ArrayList<String>(nChunks);
// Cut strings and add in the list.
for (int i = 0; i < text.length(); i+=chunkSize) {
int endIndex=i+chunksize;
if (endIndex >= text.length()) {
} else {
}
}
return chunks;
}
``````

``````public List<String> breakStringsInChunks(String text,int chunkSize) {
if (chunkSize<=1) {
throw new IllegalArgumentException("Chunk size must be positive");
}
if (text==null || text.isEmpty()) {
return Collections.emptyList();
}

List<String> chunks= new LinkedList<String>();

int index=0;
int len = text.length();

//guaranteed to succeed at least once since 0 length strings we're taken care of
do {
chunks.add(text.substring(index, Math.min(index + chunkSize, len)));
index+=chunkSize;
} while (index<len);

return chunks;
}
``````

``````public class ChunkTest extends TestCase {
public void testEmpty() throws Exception {
assertEquals(0, breakStringInChunks("", 1).size());
}

public void testOneChunk() throws Exception {
String s = "abc";
List<String> chunks = breakStringInChunks(s, s.length());
assertEquals(s, chunks.get(0));
assertEquals(1, chunks.size());
}

public void testPartialChunk() throws Exception {
String s = "abc";
List<String> chunks = breakStringInChunks(s, s.length() + 1);
assertEquals(s, chunks.get(0));
assertEquals(1, chunks.size());
}

public void testTwoChunks() throws Exception {
String s = "abc";
List<String> chunks = breakStringInChunks(s, 2);
assertEquals("ab", chunks.get(0));
assertEquals("c", chunks.get(1));
assertEquals(2, chunks.size());
}

public void testTwoEvenChunks() throws Exception {
String s = "abcd";
List<String> chunks = breakStringInChunks(s, 2);
assertEquals("ab", chunks.get(0));
assertEquals("cd", chunks.get(1));
}

private List<String> breakStringInChunks(String text, int chunkSize) {
if (text.isEmpty())
return Collections.emptyList();
int n = (text.length() + chunkSize - 1) / chunkSize;
List<String> chunks = new ArrayList<String>(n);
for (int i = 0; i < n; ++i)
chunks.add(text.substring(i * chunkSize, Math.min((i + 1) * chunkSize, text.length())));
return chunks;
}
}
``````

``````private List<String> breakStringInChunks(String text, int chunkSize)
{
List<String> chunks = new ArrayList<String>();
while (text.length() > 0)
{
if (chunkSize > text.length())
{
chunkSize = text.length();
}
text = text.substring(chunkSize);
}
return chunks;
}
``````

``````public static final List<String> chunk(final String text, final int chunkSize) {
// Figure out how many chunks we are going to make.
final int textLength = text.length();
final int numberOfChunks =
textLength % chunkSize == 0
? textLength / chunkSize
: textLength / chunkSize + 1;

// Create an array list of just the right size.
final ArrayList<String> chunks = new ArrayList<String>(numberOfChunks);

// Do all the chunking but the last one - here we know that all chunks
// are exactly chunkSize long.
for (int i = 0; i < numberOfChunks - 1; i++) {
chunks.add(text.substring(i * chunkSize, (i + 1) * chunkSize));
}

// Add final chunk, which may be shorter than chunkSize, so we use textLength
// as the end index.
chunks.add(text.substring((numberOfChunks - 1) * chunkSize, textLength));

return chunks;
}
``````

``````public static List<String> breakStringInChunks(String text, int chunkSize) {
if (chunkSize < 2) {
throw new IllegalArgumentException("Chunk size must be > 1");
}
if (null == text || text.isEmpty()) {
return Collections.emptyList();
}

List<String> chunks = new ArrayList<String>(1 + (text.length() / chunkSize));

int length = text.length() - (text.length() % chunkSize);

for (int i = 0; i < length;) {
chunks.add(text.substring(i, i += chunkSize));
}
if (length < text.length())