-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.html
228 lines (172 loc) · 9.27 KB
/
string.html
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
</body>
</html>
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.js"></script>
<script>
/*_.camelCase([string='']) 转换字符串string为 驼峰写法。*/
_.camelCase('Foo Bar');// => 'fooBar'
_.camelCase('--foo-bar--');// => 'fooBar'
_.camelCase('__FOO_BAR__');// => 'fooBar'
/*_.capitalize([string=''])转换字符串string首字母为大写,剩下为小写*/
_.capitalize('FRED');// => 'Fred'
/*_.deburr([string=''])_.deburr([string=''])转换字符串string中拉丁语-1补充字母 和 拉丁语扩展字母-A 为基本的拉丁字母,并且去除组合变音标记。*/
_.deburr('déjà vu');// => 'deja vu'
/*_.endsWith([string=''], [target], [position=string.length])检查字符串string是否以给定的target字符串结尾。*/
_.endsWith('abc', 'c');// => true
_.endsWith('abc', 'b');// => false
_.endsWith('abc', 'b', 2);// => true 位置是字符的长度
/*_.escape([string='']) 转义string中的 "&", "<", ">", '"', "'", 和 "`" 字符为HTML实体字符。*/
_.escape('fred, barney, & pebbles');
// => 'fred, barney, & pebbles'
/*_.escapeRegExp([string='']) 转义 RegExp 字符串中特殊的字符 "^", "$", "", ".", "*", "+", "?", "(", ")", "[", "]", "{", "}", 和 "|" in .*/
_.escapeRegExp('[lodash](https://lodash.com/)');
// => '\[lodash\]\(https://lodash\.com/\)'
/*_.kebabCase([string='']) 转换字符串string为 kebab case.*/
_.kebabCase('Foo Bar');// => 'foo-bar'
_.kebabCase('fooBar');// => 'foo-bar'
_.kebabCase('__FOO_BAR__');// => 'foo-bar'
/*_.lowerCase([string=''])转换字符串string以空格分开单词,并转换为小写。*/
_.lowerCase('--Foo-Bar--');// => 'foo bar'
_.lowerCase('fooBar');// => 'foo bar'
_.lowerCase('__FOO_BAR__');// => 'foo bar'
/*_.lowerFirst([string='']) 转换字符串string的首字母为小写。*/
_.lowerFirst('Fred');// => 'fred'
_.lowerFirst('FRED');// => 'fRED'
/*_.pad([string=''], [length=0], [chars=' '])如果string字符串长度小于 length 则从左侧和右侧填充字符。 如果没法平均分配,则截断超出的长度。*/
_.pad('abc', 8);// => ' abc '
_.pad('abc', 8, '_-');// => '_-abc_-_'
_.pad('abc', 3);// => 'abc'
/*_.padEnd([string=''], [length=0], [chars=' ']) 如果string字符串长度小于 length 则在右侧填充字符。 如果超出length长度则截断超出的部分。*/
_.padEnd('abc', 6);// => 'abc '
_.padEnd('abc', 6, '_-');// => 'abc_-_'
_.padEnd('abc', 3);// => 'abc'
/*_.padStart([string=''], [length=0], [chars=' '])*/
_.padStart('abc', 6);// => ' abc'
_.padStart('abc', 6, '_-');// => '_-_abc'
_.padStart('abc', 3);// => 'abc'
/*_.parseInt(string, [radix=10]) 转换string字符串为指定基数的整数。 如果基数是 undefined 或者 0,则radix基数默认是10,如果string字符串是16进制,则radix基数为 16*/
_.parseInt('08');// => 8
_.map(['6', '08', '10'], _.parseInt);// => [6, 8, 10]
/*_.repeat([string=''], [n=1])重复 N 次给定字符串。*/
_.repeat('*', 3);// => '***'
_.repeat('abc', 2);// => 'abcabc'
_.repeat('abc', 0);// => ''
/*_.replace([string=''], pattern, replacement)替换string字符串中匹配的pattern为给定的replacement */
_.replace('Hi Fred', 'Fred', 'Barney');// => 'Hi Barney'
/*_.snakeCase([string='']) 转换字符串string为 snake case.*/
_.snakeCase('Foo Bar');// => 'foo_bar'
_.snakeCase('fooBar');// => 'foo_bar'
_.snakeCase('--FOO-BAR--');// => 'foo_bar'
/*_.split([string=''], separator, [limit]) 根据separator 拆分字符串string。 */
/*[limit] (number): 限制结果的数量。*/
_.split('a-b-c', '-', 2);// => ['a', 'b']
/*_.startCase([string='']) 转换 string 字符串为 start case.*/
_.startCase('--foo-bar--');// => 'Foo Bar'
_.startCase('fooBar');// => 'Foo Bar'
_.startCase('__FOO_BAR__');// => 'FOO BAR'
/*_.startsWith([string=''], [target], [position=0])检查字符串string是否以 target 开头。*/
_.startsWith('abc', 'a');// => true
_.startsWith('abc', 'b');// => false
_.startsWith('abc', 'b', 1);// => true
/*_.template([string=''], [options={}])创建一个预编译模板方法,可以插入数据到模板中 "interpolate" 分隔符相应的位置。*/
// 使用 "interpolate" 分隔符创建编译模板
var compiled = _.template('hello <%= user %>!');
compiled({ 'user': 'fred' });
// => 'hello fred!'
// 使用 HTML "escape" 转义数据的值
var compiled = _.template('<b><%- value %></b>');
compiled({ 'value': '<script>' });
// => '<b><script></b>'
// 使用 "evaluate" 分隔符执行 JavaScript 和 生成HTML代码
var compiled = _.template('<% _.forEach(users, function(user) { %><li><%- user %></li><% }); %>');
compiled({ 'users': ['fred', 'barney'] });
// => '<li>fred</li><li>barney</li>'
// 在 "evaluate" 分隔符中使用内部的 `print` 函数
var compiled = _.template('<% print("hello " + user); %>!');
compiled({ 'user': 'barney' });
// => 'hello barney!'
// 使用 ES 分隔符代替默认的 "interpolate" 分隔符
var compiled = _.template('hello ${ user }!');
compiled({ 'user': 'pebbles' });
// => 'hello pebbles!'
// 使用自定义的模板分隔符
_.templateSettings.interpolate = /{{([\s\S]+?)}}/g;
var compiled = _.template('hello {{ user }}!');
compiled({ 'user': 'mustache' });
// => 'hello mustache!'
// 使用反斜杠符号作为纯文本处理
/* var compiled = _.template('<%= "\\<%- value %\\>" %>');
compiled({ 'value': 'ignored' });*/
// => '<%- value %>'
// 使用 `imports` 选项导入 `jq` 作为 `jQuery` 的别名
/* var text = '<% jq.each(users, function(user) { %><li><%- user %></li><% }); %>';
var compiled = _.template(text, { 'imports': { 'jq': jQuery } });
compiled({ 'users': ['fred', 'barney'] });*/
// => '<li>fred</li><li>barney</li>'
// 使用 `sourceURL` 选项指定模板的来源URL
/* var compiled = _.template('hello <%= user %>!', { 'sourceURL': '/basic/greeting.jst' });
compiled(data);*/
// => 在开发工具的 Sources 选项卡 或 Resources 面板中找到 "greeting.jst"
// 使用 `variable` 选项确保在编译模板中不声明变量
/* var compiled = _.template('hi <%= data.user %>!', { 'variable': 'data' });
compiled.source;*/
// => function(data) {
// var __t, __p = '';
// __p += 'hi ' + ((__t = ( data.user )) == null ? '' : __t) + '!';
// return __p;
// }
// 使用 `source` 特性内联编译模板
// 便以查看行号、错误信息、堆栈
/* fs.writeFileSync(path.join(cwd, 'jst.js'), '\
var JST = {\
"main": ' + _.template(mainText).source + '\
};\
');*/
/*_.toLower([string='']) 转换整个string字符串的字符为小写*/
/*_.toUpper([string=''])转换整个string字符串的字符为大写*/
/*_.trim([string=''], [chars=whitespace])从string字符串中移除前面和后面的 空格 或 指定的字符。*/
_.trim(' abc ');// => 'abc'
_.trim('-_-abc-_-', '_-');// => 'abc'
_.map([' foo ', ' bar '], _.trim);// => ['foo', 'bar']
/*_.trimEnd([string=''], [chars=whitespace])从string字符串中移除后面的 空格 或 指定的字符。*/
_.trimEnd(' abc ');// => ' abc'
_.trimEnd('-_-abc-_-', '_-');// => '-_-abc'
/*_.trimStart([string=''], [chars=whitespace])从string字符串中移除前面的 空格 或 指定的字符。*/
/*_.truncate([string=''], [options={}])截断string字符串,如果字符串超出了限定的最大值(30)。 被截断的字符串后面会以 omission 代替,omission 默认是 "..."。*/
console.log(_.truncate('hi-diddly-ho there, neighborino'));
// => 'hi-diddly-ho there, neighbo...'
console.log(_.truncate('hi-diddly-ho there, neighborino', {
'length': 24,
'separator':","/*截断点*/
}));
// => 'hi-diddly-ho there,...'
_.truncate('hi-diddly-ho there, neighborino', {
'length': 24,
'separator': /,? +/
});
// => 'hi-diddly-ho there...'
_.truncate('hi-diddly-ho there, neighborino', {
'omission': ' [...]'
});
// => 'hi-diddly-ho there, neig [...]'
/*_.unescape([string=''])这个方法转换string字符串中的 HTML 实体 &, <, >, ", ', 和 ` 为对应的字符。 */
/*不会转换其他的 HTML 实体,需要转换可以使用类似 he 的第三方库*/
_.unescape('fred, barney, & pebbles');
// => 'fred, barney, & pebbles'
/*_.upperCase([string=''])转换字符串string为 空格分隔的大写单词。*/
_.upperCase('--foo-bar');// => 'FOO BAR'
_.upperCase('fooBar');// => 'FOO BAR'
_.upperCase('__foo_bar__');// => 'FOO BAR'
/*_.upperFirst([string='']) 转换字符串string的首字母为大写。*/
_.upperFirst('fred');// => 'Fred'
_.upperFirst('FRED');// => 'FRED'
/*_.words([string=''], [pattern])拆分字符串string中的词为数组 。*/
_.words('fred, barney, & pebbles');// => ['fred', 'barney', 'pebbles']
_.words('fred, barney, & pebbles', /[^, ]+/g);// => ['fred', 'barney', '&', 'pebbles']
</script>