Skip to content

ProtoString wordWrap() Method

Seth Clydesdale edited this page Jun 14, 2015 · 1 revision

<< Back to Miscellaneous Methods

Examples

Passing a number as the first parameter allows you to define the amount of characters on each line before creating a new one.

'Want to go out for a cup of coffee?'.wordWrap(20);

/* return value :
Want to go out for a 
cup of coffee?
*/

Passing a string as the second parameter allows you to change the newline character.

'Want to go out for a cup of coffee?'.wordWrap(20, '<br/>');

/* return value :
Want to go out for a <br/>cup of coffee?
*/

Passing true as the final parameter will break words when the line width limit has been reached.

'Want to go out for a cup of coffee?'.wordWrap(17, '\n', true);

/* return value :
Want to go out f
or a cup of coffe
e?
*/

Description

The wordWrap method allows you to adjust the amount of characters allowed on each line. A newline will be created once the specified width is reached. If cut is set to true it will break the word to the next line regardless if it was finished.

Syntax

String.wordWrap(width, newline, cut);

Parameters

Parameter Description
width A number that defines the amount of characters allowed on each line before a newline is created. Defaults to 50 if left undefined.
newline A string that defines the character(s) used to separate each line. Defaults to \n if left undefined.
cut A boolean value that defines whether or not words should be broken. Setting this parameter to true will break the characters of a word to a newline, regardless of whether or not it was finished. Defaults to waiting for words to be finished before inserting a newline.
Clone this wiki locally