-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# 字符串ES6中的新方法 | ||
|
||
``` | ||
const id = '422201199201031254x'; | ||
const flag = 'I love code'; | ||
``` | ||
|
||
### `.startsWith()` | ||
是否已某个字符开头,如果有第二个参数,表示从第几个字符开始,这个方法大小写敏感 | ||
``` | ||
id.startsWith('4'); // true | ||
id.startsWith('1993', 6'); // true | ||
``` | ||
|
||
### `.endsWith()` | ||
是否已某个字符结尾,如果有第二个参数,表示从第几个字符开始,这个方法大小写敏感 | ||
``` | ||
id.endsWith('x'); // true | ||
``` | ||
|
||
### `.includes()` | ||
是否包含某个字符,如果有第二个参数,表示从第几个字符开始,这个方法大小写敏感 | ||
``` | ||
flag.includes('code'); // true | ||
flag.includes('code',3); // true | ||
``` | ||
|
||
### `.repeat()` | ||
重复某个字符 | ||
'哈'.repeat(3); // 哈哈哈 | ||
|
||
``` | ||
//右对齐 | ||
function padder(string, length = 25) { | ||
return `${' '.repeat(length - string.length)}${string}`; | ||
} | ||
``` |