Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
public String wordWrap( String s, int lineLen ) {
int charsThisLine = 0;
int totalChars = 0;
String lhs = new String();
FullBreak:{
while ( totalChars < s.length() ) {
// Still some string to wrap
while ( charsThisLine < totalChars + lineLen ) {
charsThisLine = s.indexOf(' ', charsThisLine+1);
if(charsThisLine==-1)
break FullBreak;
}
// We've run off the end of the line, so
// back up, insert a new line, and continue
charsThisLine = s.lastIndexOf(' ', charsThisLine-1);
// Keep the space at the end of the line rather
// than the beginning of the next line
charsThisLine++;
lhs =lhs + s.substring(totalChars, charsThisLine - 1) + "\n\r";
//s = lhs + '\n' + s.substring(charsThisLine);
totalChars = charsThisLine;
//charsThisLine = totalChars;
//lhs = "";
}
}//end FullBreak
lhs =lhs + s.substring(totalChars);
return lhs;
}
public String WordWrap( String s, int lineLen ) {
// Inserts a newline character before every
// (char)12 and (char)10(char)13 as well as
// before every word that causes the lenght
// of a line to exceed lineLen
// If a single word exceeds lineLen, it is
// preserved on its own line.
int charsThisLine = 0;
int totalChars = 0;
char c;
String lhs;
for ( int i = 0; i < s.length(); i++ ) {
// Still some string to wrap
c = s.charAt(i);
if (( c == 12 ) || (( c == 10 ) && ( s.charAt(i+1) == 13))) {
// new line
lhs = s.substring(0, i);
s = lhs + '\n' + s.substring(i);
totalChars = i++;
charsThisLine = 0;
continue;
}
// Surpassed the end of the line
if ( charsThisLine > lineLen ) {
// back up to the last space
i = s.lastIndexOf(' ', i - 1);
// If there is no earlier space
if ( i == -1 ) {
// Then the first word of the string
// is huge, and we will break at the
// next space
i = s.indexOf(' ', totalChars);
// If there is no later space
if ( i == -1 ) {
// The string is one huge word
// so break at the end
i = s.length();
}
// There is an earlier space
// If the space is right in front
// of a newline, then we have a huge
// word on this line
} else if ( s.charAt(i+1) == '\n') {
// So break on the next space
i = s.indexOf(' ', i + 1);
// If there is no later space
if ( i == -1) {
// The last word is huge and we
// will break at the end of the line
i = s.length();
} else {
i++;
}
// There is an earlier space
// It is not in front of a newline
} else {
i++;
}
// Insert the newline
lhs = s.substring(0, i);
s = lhs + '\n' + s.substring(i);
totalChars = i++;
charsThisLine = 0;
continue;
}
charsThisLine++;
}
return s;
}
public static String WordWrap( String s, int lineLen, boolean hyphen ) {
StringBuffer str = new StringBuffer(s);
// Inserts a newline character before every
// (char)12 and (char)10(char)13 as well as
// before every word that causes the lenght
// of a line to exceed lineLen
// If a single word exceeds lineLen, it is
// preserved on its own line.
int charsThisLine = 0;
int totalChars = 0;
int lastSpace = 0;
char c;
int i = 0;
while( i < str.length() ) {
c = str.charAt(i);
if ( c == ' ' ) lastSpace = i;
// Insert a new line before (char)12 or
// (char)10(char)13
if (( c == 12 ) || (( c == 10 ) && ( i + 1 < str.length() ) &&
( str.charAt( i + 1 ) == 13 ))) {
str.insert( i++, '\n');
i++;
charsThisLine = 0;
lastSpace = 0;
totalChars = i;
continue;
}
// End of the line, we must break
if (charsThisLine++ > lineLen) {
// If we exceeded the line length with a space
// we have to find the previous space
if ( lastSpace == i ) {
for( int j = i - 1; j > 0; j-- ) {
if ( str.charAt(j) == ' ' ) {
lastSpace = j;
break;
}
}
}
// lastSpace points to the space after which we
// will break. If the next character is already
// a newline, then set lastSpace = 0 to insert
// a hyphen or break at the next space.
if ( lastSpace > 0 ) {
if (( str.length() > lastSpace + 1) && ( str.charAt(lastSpace + 1) != '\n' )) {
str.insert(++lastSpace, '\n');
i = lastSpace;
charsThisLine = 0;
lastSpace = 0;
totalChars = i + 1;
continue;
} else {
lastSpace = 0;
}
}
if ( lastSpace == 0) {
// If hyphen == true, then insert a hyphen
// otherwise break at the next space
if ( hyphen ) {
str.insert( totalChars + lineLen - 1, '-' );
str.insert( totalChars + lineLen, '\n' );
i = totalChars + lineLen;
charsThisLine = 0;
lastSpace = 0;
totalChars = i;
continue;
} else {
// Insert at the next space
for( int j = totalChars; j < str.length(); j++ ) {
if ( str.charAt(j) == ' ' ) {
lastSpace = j;
break;
}
}
// lastSpace points to either the space
// after which we will insert the newline
// or the end of the string
if ( lastSpace == str.length() ) {
str.insert(lastSpace, '\n');
} else {
str.insert(++lastSpace, '\n');
}
i = lastSpace;
charsThisLine = 0;
lastSpace = 0;
totalChars = i;
continue;
}
}
}
i++;
}
return str.toString();
}