Here you can find the source of splitString(String string, int maxCharsPerLine)
Parameter | Description |
---|---|
string | a parameter |
maxCharsPerLine | a parameter |
public static List<String> splitString(String string, int maxCharsPerLine)
//package com.java2s; /**/*from w w w . jav a2s. c o m*/ * Manipulate and analyze Strings. * * <p> * <span class="BSDLicense"> This software is distributed under the <a * href="http://hci.stanford.edu/research/copyright.txt">BSD License</a>. </span> * </p> * * @author <a href="http://graphics.stanford.edu/~ronyeh">Ron B Yeh</a> (ronyeh(AT)cs.stanford.edu) */ import java.util.ArrayList; import java.util.List; public class Main { /** * Splits a string into multiple lines, each with at most maxChars characters. We will try our best to * split after spaces. * * @param string * @param maxCharsPerLine * @return a List of Strings representing the lines. */ public static List<String> splitString(String string, int maxCharsPerLine) { final List<String> lines = new ArrayList<String>(); final String[] items = string.split(" "); final StringBuilder currString = new StringBuilder(); for (String item : items) { if (currString.length() + item.length() > maxCharsPerLine) { lines.add(currString.toString()); currString.setLength(0); // clear buffer } currString.append(item + " "); } lines.add(currString.toString()); return lines; } }