简体   繁体   中英

javascript - how to break up a string every X amount of characters?

How do I break up a string every X amount of characters? For example, I'd like to break up a very long string every 1000 characters, and the string could be completely random everytime.

var string = <my text string that is thousands of characters long>

You could use Regex :

'asdfasdfasdfasdf'.match(/.{3}|.{1,2}/g); // 'asd', 'fas', etc.

Replace 3 with 1000 of course.

Here's a contrived example: http://jsfiddle.net/ReRPz/1/


As a function:

 function splitInto(str, len) { var regex = new RegExp('.{' + len + '}|.{1,' + Number(len-1) + '}', 'g'); return str.match(regex ); } 

That RegExp really only needs to be created once if you have a set number to split like 1000.

Try this function:

function getParts(str, len)
{
    var res = [];
    ​while (str.length) {
        res.push(str.substring(0, len));
        str = str.substring(len);
    }
    return res;
}
var s = "qweasedzxcqweasdxzc12";
console.log(getParts(s, 10));

Like this:

var myString = "my text string that is thousands of characters long";
var myCurrentString = "";
var myBrokenUpString = new Array();

for(var i = 0; i < myString.length; i++) {
    myCurrentString += myString.charAt(i);

    if(i % 1000 == 0) {
        myBrokenUpString.push(myCurrentString);
        myCurrentString = "";
    }

    if(i + 1 == myString.length) myBrokenUpString.push(myCurrentString);
}

Please note that the above code is untested and may contain errors.

You can then POST the array and piece it back together on the other end. The piecing-together-code would look something like this:

var myRestoredString = "";
for(var i = 0; i<myBrokenUpString.length; i++)  {
    myRestoredString += myBrokenUpString[i];
}

i would use substring function on string

     str = //the given string
     arr = [];

     for(i=0;i<str.length;i+=1000)
     {
        s = i;
        // if the last string is less than 1000chars
        e = (str.length - i) > 1000 ? (i+1000) : (str.length - i); 
        arr.push = str.substring(s,e);
     }

Here's a way to do it recursively:

var string = "my text string that is thousands of characters long";

var arr = [];

function div( str, len ) {

    if( str.length < len )
        return arr.push(str);
    else
        arr.push(str.substring(0,len))

    div( str.substring(len), len );
}

div( string, 5 );

for( var i = 0; i < arr.length; i++ ) {
    document.write( arr[i] + "<br/>");  
}

/* output:

my te
xt st
ring 
that 
is th
ousan
ds of
char
acter
s lon
g


*/

Borrowing the idea from Joe Tuskan:

var str = 'asdfasdfasdfasdf';
var len = 3;
var regex = new RegExp(".{"+len+"}", "g");
var trail = str.length - (str.length % len);

var parts = str.match(regex);
parts.push(str.substring(trail));

document.write(parts.join('<br>'));

http://jsfiddle.net/6aSHB/1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM