Skip to content

Commit

Permalink
ES6字符串新方法
Browse files Browse the repository at this point in the history
  • Loading branch information
dptms committed Oct 23, 2017
1 parent 75ceda0 commit 5bcc34e
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions 5.new-string-methods.md
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}`;
}
```

0 comments on commit 5bcc34e

Please sign in to comment.