Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js text word wrap

str = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It w as popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";

str = wordWrap(str, 40);

function wordWrap(str, maxWidth) {
    var newLineStr = "
"; done = false; res = '';
    while (str.length > maxWidth) {                 
        found = false;
        // Inserts new line at first whitespace of the line
        for (i = maxWidth - 1; i >= 0; i--) {
            if (testWhite(str.charAt(i))) {
                res = res + [str.slice(0, i), newLineStr].join('');
                str = str.slice(i + 1);
                found = true;
                break;
            }
        }
        // Inserts new line at maxWidth position, the word is too long to wrap
        if (!found) {
            res += [str.slice(0, maxWidth), newLineStr].join('');
            str = str.slice(maxWidth);
        }

    }

    return res + str;
}

function testWhite(x) {
    var white = new RegExp(/^s$/);
    return white.test(x.charAt(0));
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: prependchild in javascript 
Javascript :: find space in string js 
Javascript :: convert new date to minutes number javascript 
Javascript :: gradlew command not found react native 
Javascript :: js input type range get value on select 
Javascript :: iframe content fetching 
Javascript :: disable scroll on modal open 
Javascript :: js number to scientific notation 
Javascript :: jquery if .val is blank 
Javascript :: get index of item array 
Javascript :: on enter key press react js 
Javascript :: request entity too large 
Javascript :: react-native android build apk 
Javascript :: javascript remove all spaces 
Javascript :: fetch api javascript 
Javascript :: prepend to array javascript 
Javascript :: javascript average of arguments 
Javascript :: toggle class javascript stack overflow 
Javascript :: react navigation header background color 
Javascript :: remove a object name from spread operator 
Javascript :: alphabet string javascript 
Javascript :: $post in jquery 
Javascript :: closest javascript 
Javascript :: MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string. 
Javascript :: javascript map max value 
Javascript :: moment js difference between two dates 
Javascript :: Write the JavaScript code to set the width of element to 50%; 
Javascript :: how to define state in react function 
Javascript :: window.location 
Javascript :: 2d array to 1d array javascript 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =