Skip to content

Instantly share code, notes, and snippets.

@csesteban
Forked from unclebob/Wrapper.java
Created October 9, 2011 18:43
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 csesteban/1274003 to your computer and use it in GitHub Desktop.
Save csesteban/1274003 to your computer and use it in GitHub Desktop.
Sometimes less is more ;)
public class Wrapper {
public static String wrap(String s, int col) {
return new Wrapper(col).wrap(s);
}
private int col;
private Wrapper(int col) {
this.col = col;
}
private String wrap(String s) {
if (s.length() <= col)
return s;
int space = (s.substring(0, col + 1).lastIndexOf(' '));
if (space != -1)
return s.substring(0, space) + "\n" + wrap(s.substring(space + 1));
else
return s.substring(0, col) + "\n" + wrap(s.substring(col));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment