Skip to content

Instantly share code, notes, and snippets.

@unclebob
Created October 3, 2010 19:29
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 unclebob/608842 to your computer and use it in GitHub Desktop.
Save unclebob/608842 to your computer and use it in GitHub Desktop.
public class Wrapper {
public static String wrap(String s, int col) {
if (s.length() <= col)
return s;
else {
StringBuilder builder = new StringBuilder();
String[] strings = s.split(" ");
int column = 0;
for (String segment : strings) {
column += segment.length();
if (column < col)
builder.append(segment+" ");
else
builder.append(segment+"\n");
}
return builder.toString();
}
// return s.replaceAll(" ", "\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment