JavaScript insert new line to any space in the middle of string -
i insert 1 new line character in middle of string:
for (var i=0; i<labels.length; i++){ if (labels[i].length > 30) { //the split occur here } }
is there js function this?
any ideas?
edit
to replace spaces in string new line:
string.replace(/ /g, '\n');
/ /g
refers global replace of spaces found.
say string follows:
var string = 'the quick brown fox jumps on lazy dog';
you need find length of string, middle point, , nearest space middle:
var length = string.length; var middle = math.round(length / 2); var spacenearmiddle = string.indexof(' ', middle); var string1 = string.substring(0, spacenearmiddle); var string2 = string.substring(spacenearmiddle + 1, length);
the result of string1 , string2 "the quick brown fox" , "jumps on lazy dog".
Comments
Post a Comment