File tree Expand file tree Collapse file tree 2 files changed +73
-1
lines changed
02-Part II - ES2015 features/09-String additions Expand file tree Collapse file tree 2 files changed +73
-1
lines changed Original file line number Diff line number Diff line change 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+ ```
Original file line number Diff line number Diff line change @@ -53,7 +53,7 @@ Other useful references:
535306 . Classes
545407 . Modules
555508 . Promises
56- 09 . String additions ( _ in progress _ )
56+ 09 . String additions
575710 . Array additions (_ in progress_ )
585811 . Generators (_ in progress_ )
595912 . Sets (_ in progress_ )
You can’t perform that action at this time.
0 commit comments