Created
October 3, 2010 20:55
-
-
Save unclebob/608930 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void wrapJustBeforeWordBoundary() throws Exception { | |
assertThat(wrap("word word", 4), equalTo("word\nword")); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Wrapper { | |
public static String wrap(String s, int col) { | |
if (s.length() <= col) | |
return s; | |
int space = (s.substring(0, col).lastIndexOf(' ')); | |
if (space != -1) | |
return (s.substring(0, space) + "\n" + wrap(s.substring(space + 1), col)); | |
else if (s.charAt(col) == ' ') | |
return (s.substring(0, col) + "\n" + wrap(s.substring(col + 1), col)); | |
else | |
return (s.substring(0, col) + "\n" + wrap(s.substring(col), col)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment