programming-examples/js/String/Chop a string into chunks of a specific length.js
2019-11-15 12:59:38 +01:00

9 lines
341 B
JavaScript

string_chop = function(str, size){
if (str == null) return [];
str = String(str);
size = ~~size;
return size > 0 ? str.match(new RegExp('.{1,' + size + '}', 'g')) : [str];
}
console.log(string_chop('w3resource'));
console.log(string_chop('w3resource',2));
console.log(string_chop('w3resource',3));