1
|
|
|
|
2
|
|
|
var stringFuncs = { |
3
|
|
|
|
4
|
|
|
getAfterChar: function(char, inclusive = false){ |
5
|
|
|
var str = this; |
6
|
|
|
var count = str.length; |
7
|
|
|
if (!inclusive) { |
8
|
|
|
char++; |
9
|
|
|
} |
10
|
|
|
if (char >= count) { |
11
|
|
|
return ""; |
12
|
|
|
} |
13
|
|
|
return str.substr(char); |
14
|
|
|
}, |
15
|
|
|
getBeforeChar: function(char, inclusive = false){ |
16
|
|
|
var str = this; |
17
|
|
|
var count = str.length; |
|
|
|
|
18
|
|
|
if (inclusive) { |
19
|
|
|
char--; |
20
|
|
|
} |
21
|
|
|
if (char <= 0) { |
22
|
|
|
return ""; |
23
|
|
|
} |
24
|
|
|
return str.substr(0, char); |
25
|
|
|
}, |
26
|
|
|
|
27
|
|
|
splitChars: function(){ |
28
|
|
|
var text = this; |
29
|
|
|
|
30
|
|
|
// split to array of characters |
31
|
|
|
return text.split(''); |
32
|
|
|
/*var chars:Array = []; |
33
|
|
|
for (var c = 0, cl = text.length;c<cl;c++){ |
34
|
|
|
chars[c] = text.charAt(c); |
35
|
|
|
} |
36
|
|
|
return chars;*/ |
37
|
|
|
}, |
38
|
|
|
splitCharCodes: function(){ |
39
|
|
|
var text = this; |
40
|
|
|
|
41
|
|
|
// split to array of char codes |
42
|
|
|
var chars = []; |
43
|
|
|
for (var c = 0, cl = text.length;c<cl;c++){ |
44
|
|
|
chars[c] = text.charCodeAt(c); |
45
|
|
|
} |
46
|
|
|
return chars; |
47
|
|
|
}, |
48
|
|
|
splitCharPairs: function(){ |
49
|
|
|
var str = this; |
50
|
|
|
|
51
|
|
|
// return an Array of 2-character Strings |
52
|
|
|
|
53
|
|
|
// split words by space |
54
|
|
|
var words = str.split(" "); |
55
|
|
|
|
56
|
|
|
// calculate array of char pairs |
57
|
|
|
var allPairs = []; |
58
|
|
|
|
59
|
|
|
for (var w = 0;w<words.length;w++){ |
60
|
|
|
|
61
|
|
|
// if not blank |
62
|
|
|
if(words[w]!= ''){ |
63
|
|
|
|
64
|
|
|
// Find the pairs |
65
|
|
|
var word = words[w]; |
66
|
|
|
var numPairs = word.length - 1; |
67
|
|
|
|
68
|
|
|
// add to list of pairs |
69
|
|
|
for (var i = 0;i<numPairs;i++){ |
70
|
|
|
allPairs[i] = word.substr(i,2); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
return allPairs; |
75
|
|
|
}, |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
removeEdgeChars: function(charsToRemove){ |
79
|
|
|
var str = this; |
80
|
|
|
if ((str.length - (charsToRemove * 2)) <= 0) { |
81
|
|
|
return ""; |
82
|
|
|
} |
83
|
|
|
return str.substring(charsToRemove, str.length - charsToRemove); |
84
|
|
|
}, |
85
|
|
|
|
86
|
|
|
setLength: function(count, padOnRight = true, padding = " "){ |
87
|
|
|
var text = this; |
88
|
|
|
if (text.length == count) { |
89
|
|
|
return text; |
90
|
|
|
} |
91
|
|
|
if (text.length >= count) { |
92
|
|
|
return text.substr(0, count); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return padOnRight ? S.PadRight(text, padding, count) : S.PadLeft(text, padding, count); |
|
|
|
|
96
|
|
|
}, |
97
|
|
|
atMost: function(count, leftAlign = true){ |
98
|
|
|
var text = this; |
99
|
|
|
if (text.length > count) { |
100
|
|
|
if (leftAlign){ |
101
|
|
|
return text.substr(0, count); |
102
|
|
|
}else{ |
|
|
|
|
103
|
|
|
return text.substr((text.length - count), count); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
return text; |
107
|
|
|
}, |
108
|
|
|
removeFirstXChars: function(charNum){ |
109
|
|
|
var str = this; |
110
|
|
|
if (str.length <= charNum) { |
111
|
|
|
return ""; |
112
|
|
|
} |
113
|
|
|
return str.substring(charNum, str.length); |
114
|
|
|
}, |
115
|
|
|
removeLastXChars: function(charNum){ |
116
|
|
|
var str = this; |
117
|
|
|
if (str.length <= charNum) { |
118
|
|
|
return ""; |
119
|
|
|
} |
120
|
|
|
return str.substring(0, str.length - charNum); |
121
|
|
|
}, |
122
|
|
|
getFirstXChars: function(numOfChars){ |
123
|
|
|
var text = this; |
124
|
|
|
return text.getRange(0, numOfChars - 1); |
125
|
|
|
}, |
126
|
|
|
getLastXChars: function(numOfChars){ |
127
|
|
|
var text = this; |
128
|
|
|
return text.getRange(text.length - numOfChars, text.length - 1); |
129
|
|
|
}, |
130
|
|
|
setFirstXChars: function(numOfChars, newRange){ |
131
|
|
|
var text = this; |
132
|
|
|
return S.SetRange(text, 0, numOfChars - 1, newRange); |
|
|
|
|
133
|
|
|
}, |
134
|
|
|
setLastXChars: function(numOfChars, newRange){ |
135
|
|
|
var text = this; |
136
|
|
|
return S.SetRange(text, text.length - 1 - numOfChars, text.length - 1, newRange); |
|
|
|
|
137
|
|
|
}, |
138
|
|
|
|
139
|
|
|
none:null |
140
|
|
|
}; |
141
|
|
|
|
142
|
|
|
// register funcs |
143
|
|
|
UB.registerFuncs(String.prototype, stringFuncs); |
|
|
|
|