-
Notifications
You must be signed in to change notification settings - Fork 0
/
#Strings.gs
40 lines (34 loc) · 890 Bytes
/
#Strings.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* Splits string into equal parts.
*
* @param {string} str The string to chunk.
* @param {number} length The length of parts.
* @return Array - string splitted into parts with len = length.
* @customfunction
*/
function chunkString(str, length) {
return str.match(new RegExp('.{1,' + length + '}', 'g'));
}
function TESTchunkString() {
var str = '12312312';
Logger.log(chunkString(str, 2));
}
function chunkStringParts(str, parts) {
var len = str.length;
// length of a part
var length = Math.ceil(len / parts);
return chunkString(str, length);
}
function TESTchunkStringParts() {
var str = '12312312';
Logger.log(chunkStringParts(str, 3));
}
function byteCount(str) {
return encodeURI(str).split(/%..|./).length - 1;
}
/*
works like function REGEXREPLACE
*/
function replaceAll(str, find, replace) {
return str.replace(new RegExp(find, 'g'), replace);
}