Wrap Word
import java.text.BreakIterator;
import java.util.Locale;
public class Main {
public static void main(String[] argv){
System.out.println(wordWrap("this is a test", 10, Locale.CANADA) );
}
public static String wordWrap(String input, int width, Locale locale) {
if (input == null) {
return "";
} else if (width < 5) {
return input;
} else if (width >= input.length()) {
return input;
}
StringBuilder buf = new StringBuilder(input);
boolean endOfLine = false;
int lineStart = 0;
for (int i = 0; i < buf.length(); i++) {
if (buf.charAt(i) == '\n') {
lineStart = i + 1;
endOfLine = true;
}
if (i > lineStart + width - 1) {
if (!endOfLine) {
int limit = i - lineStart - 1;
BreakIterator breaks = BreakIterator.getLineInstance(locale);
breaks.setText(buf.substring(lineStart, i));
int end = breaks.last();
// if the last character in the search string isn't a space,
// we can't split on it (looks bad). Search for a previous
// break character
if (end == limit + 1) {
if (!Character.isWhitespace(buf.charAt(lineStart + end))) {
end = breaks.preceding(end - 1);
}
}
// if the last character is a space, replace it with a \n
if (end != BreakIterator.DONE && end == limit + 1) {
buf.replace(lineStart + end, lineStart + end + 1, "\n");
lineStart = lineStart + end;
}
// otherwise, just insert a \n
else if (end != BreakIterator.DONE && end != 0) {
buf.insert(lineStart + end, '\n');
lineStart = lineStart + end + 1;
} else {
buf.insert(i, '\n');
lineStart = i + 1;
}
} else {
buf.insert(i, '\n');
lineStart = i + 1;
endOfLine = false;
}
}
}
return buf.toString();
}
}
Home
Java Book
Runnable examples
Java Book
Runnable examples
Data Type String:
- Compare String with compareTo method
- Compare strings ignoring case
- Compare Strings with equals
- Compare string with a StringBuffer
- Convert Primitive Type Value to a String
- Convert String value to byte
- Convert String to primitive byte by parseByte
- Convert String to byte
- Convert String to byte array
- Convert String to Boolean
- Convert string to char array
- Convert String to double
- Convert String to float
- Convert binary string into integer
- Convert String to int
- Convert String to Long
- Convert a string to a number
- Convert String to Short
- Convert hex String To Byte Array
- Convert Octal string to integer
- Convert String to Lower Case
- Convert String to Upper Case
- Convert string to uppercase for a Locale
- Convert characters in a string to a Set
- Capitalize/Uncapitalize the first character converted to uppercase
- Capitlize each word in a string (journal titles, etc)
- Count letters in a String
- Count numbers in a String
- Count the number of occurrences of character c in a string.
- Check each char value from a string
- Count substring Occurrences
- Convert char in a String to Ascii code
- Convert Character array to String
- Encode a string in UTF-8
- Encode string in UTF-8 byte[]
- Encode UTF-8 from Unicode
- Escape string
- Format strings into table
- Format string to Center
- Justify left string
- Justify right string
- Length of things
- Hash Code String
- Is String is a Legal Java Identifier
- Is String empty (""), null and whitespace
- Is a string a palindrome
- Pad right a string
- Pad left a string
- Remove a substring
- Remove leading white space from a string
- Remove specified chars from a string
- Remove trailing whitespace
- Replace Characters in a String
- Replace multiple whitespaces between words with single blank
- Replace first occurrences of given String with new one
- Replace all occurrences of given String with new one and returns new String object.
- Replace new line sign
- Replace a character at a specified position
- Reverse a string
- Search a substring
- Search a Substrings for First occurrence
- Search a substring for Last occurrence
- Search substring anywhere, ignore case( regular expressions )
- ends With string
- Ends with, ignore case( regular expressions )
- starts With string
- Starts with, ignore case( regular expressions )
- starts with a digit or uppercase letter
- Split string by a whitespace character
- Split string by space
- Split string by - (hyphen)
- Split string by dot
- Split string by | (bar)
- Split string by , (comma)
- Split string for the maximum number of substrings
- Split with regular expression
- Split string by Pattern for space splittor
- Split Strings with -/%s
- Split string on word boundaries.
- Split string on commas and zero or more spaces.
- Split string on word boundaries, but allow embedded periods and @.
- Split string on punctuation and zero or more trailing spaces.
- Split string with and's and or's
- Sort string array in case insensitive order and case sensitive order
- Substring
- Trim leading and trailing space from String
- Trim double quotes
- Unquote string
- Wrap Word
- Break Lines
- Abbreviates a String using ellipses.
- Delimit a string
- Unaccent letters