Skip to content

Commit 5784531

Browse files
committed
string additions completed.
1 parent 1fe37a9 commit 5784531

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
## String additions
2+
3+
```js
4+
// ES5 example
5+
var title1 = 'Red Rising.';
6+
if (title1.indexOf('Red' === 0)) {
7+
console.log('existed!');
8+
}
9+
```
10+
11+
* ES6 new string methods:
12+
13+
* .includes()
14+
* .startsWith()
15+
* .endsWith()
16+
* .repeat()
17+
18+
#### .includes()
19+
20+
```js
21+
let title2 = 'Red Rising.';
22+
if (title2.includes('ising')) {
23+
console.log('existed!');
24+
}else{
25+
console.log('NOT existed!');
26+
}
27+
```
28+
29+
#### .startsWith()
30+
31+
```js
32+
let title = 'Red Rising.';
33+
if (title.startsWith('Ri',4)) { // the second arg is optional and it shows the start index - position - (note that it's CASE-SENSITIVE )
34+
console.log('existed!');
35+
}else{
36+
console.log('NOT existed!');
37+
}
38+
```
39+
40+
#### .endsWith()
41+
42+
```js
43+
let title3 = 'Red Rising.';
44+
if (title3.endsWith('ing.')) {
45+
console.log('existed!');
46+
}else{
47+
console.log('NOT existed!');
48+
}
49+
```
50+
51+
#### .repeat()
52+
53+
```js
54+
let str = 'lol';
55+
console.log(str.repeat(5)); // lollollollollol
56+
57+
let str1 = 'Hello';
58+
let str2 = 'o';
59+
console.log(
60+
// str1 + str2.repeat(10) + '.'
61+
`${str1}${str2.repeat(10)}.` // Hellooooooooooo.
62+
);
63+
```
64+
65+
* another example
66+
67+
```js
68+
let heading = 'This heading is here';
69+
console.log(
70+
`${'=>'.repeat(5)} ${heading} ${'<='.repeat(5)}` // =>=>=>=>=> This heading is here <=<=<=<=<=
71+
)
72+
```

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Other useful references:
5353
06. Classes
5454
07. Modules
5555
08. Promises
56-
09. String additions (_in progress_)
56+
09. String additions
5757
10. Array additions (_in progress_)
5858
11. Generators (_in progress_)
5959
12. Sets (_in progress_)

0 commit comments

Comments
 (0)