programming-examples/js/String/Chop a string into chunks of a specific length.js

9 lines
341 B
JavaScript
Raw Normal View History

2019-11-15 12:59:38 +01:00
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));