admin管理员组

文章数量:1315482

In javascript how to break my string if it exceeds 25 characters into two lines , if my string contain 75 character i want to get the string to three lines of 25 character.

thanks in advance

In javascript how to break my string if it exceeds 25 characters into two lines , if my string contain 75 character i want to get the string to three lines of 25 character.

thanks in advance

Share Improve this question asked Feb 2, 2011 at 6:19 kirankiran 513 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 7

That's really easy to acplish with a regular expression:

var text = '75 characters long (really!) — well... maybe not, but you get the picture.',
    broken;
broken = text.replace(/([^\0]{25})/g, '$1\n');

As demonstrated here: http://jsbin./ajiyo/3.

Edit: To explain the regular expression: it will match any string of characters (the collection of every character except NUL), that is 25 characters long.

The parentheses () mean that this portion should be captured, and the '$1' part of the second argument (the replacement string) refers to that first capture.

Every string of 25 characters found will be replaced by 'itself plus a newline'. If the remainder is less than 25 characters, it will not be matched but left alone.

2nd edit: Brock is right, the dot loses its special meaning when in square brackets. I've replaced that by all non-NUL characters, since I don't expect NUL characters in a text string.

Try to use something like this

var point=0;

var myStr="12345678901234567890ABCDE my very long string 12345678901234567890ABCDE";
var myRes="";
while(myStr.substring(point).length>25)
{
  myRes=myRes+myStr.substring(point,point+25)+"\n"
  point+=25;
}

return myRes+myStr.substring(point);

This should get you pretty close:

var txt = "This is a really long string that should be broken up onto lines of 25 characters, or less.";

for (i=0;i<(Math.ceil(txt.length/25));i++) {
    document.write(txt.substring(25*i,25*(i+1)) + "<br />");
}

See working example:

http://jsfiddle/dbgDj/

use str_split (php) equivalent in javascript

http://phpjs/functions/str_split:530

 function str_split (string, split_length) {
    // Convert a string to an array. If split_length is specified,
    // break the string down into chunks each split_length characters long.  
    // 
    // version: 1101.3117
    // discuss at: http://phpjs/functions/str_split    
    // +     original by: Martijn Wieringa
    // +     improved by: Brett Zamir (http://brett-zamir.me)
    // +     bugfixed by: Onno Marsman
    // +      revised by: Theriault
    // +        input by: Bjorn Roesbeke (http://www.bjornroesbeke.be/)    
    // +      revised by: Rafał Kukawski (http://blog.kukawski.pl/)
    // *       example 1: str_split('Hello Friend', 3);
    // *       returns 1: ['Hel', 'lo ', 'Fri', 'end']

    if (split_length === null) {
        split_length = 1;    }
    if (string === null || split_length < 1) {
        return false;
    }
    string += '';
    var chunks = [], pos = 0, len = string.length;
    while (pos < len) {
        chunks.push(string.slice(pos, pos += split_length));
    }
    return chunks;
}

本文标签: javascriptbreaking a string into different linesStack Overflow