Strings in javascript
./code/strings.js
1let s = "abcdefg";
2console.log(s.length); // 7
3console.log(`s is ${s}`); // s is abcdefg
4
5// start 1 (inclusive)
6// end 4 (exclusive)
7console.log(s.substring(1, 4)); // bcd
8console.log(s.slice(1, 4)); // bcd
9
10// last 3 characters
11console.log(s.slice(-3)); // efg
12
13// start 3 (inclusive)
14// to the end of the string
15console.log(s.slice(3)); // defg
16
17// split using delimiter 'd'
18console.log(s.split('d')) // ['abc', 'efg']
19
20// split using delimiter 'de'
21console.log(s.split('de')) // ['abc', 'fg']
22
23s = "1232526314";
24// note: the delimiter is the whole string '23`
25console.log(s.split('23')) // ['1', '2526314']
26
27console.log(s.split('2')) // ['1', '3', '5', '6314']
28
29s = "abcdefga";
30console.log(s.indexOf('a')); // 0, only the first occurrence
31console.log(s.indexOf('a', 2)); // 7, search starting from the 2nd position
32console.log(s.indexOf('c')); // 2
33console.log(s.indexOf('c', 1)); // 2
34console.log(s.indexOf('c', 2)); // 2
35console.log(s.indexOf('c', 3)); // -1, not found
36console.log(s.indexOf('x')); // -1
37
38console.log(s.lastIndexOf('a')); // 7
39console.log(s.indexOf('bc')); // 1
40
41console.log(s.startsWith('a')); // true
42console.log(s.startsWith('ab')); // true
43console.log(s.startsWith('ac')); // false
44
45console.log(s.endsWith('a')); // true
46console.log(s.endsWith('ga')); // true
47console.log(s.endsWith('da')); // false
48
49// include a substring
50console.log(s.includes('de')); // true
51console.log(s.includes('ded')); // false
52
53// s is not changed. It return a new string
54console.log(s.replace('ab', 'AB')); // ABcdefga
55console.log(s.toUpperCase()); // ABCDEFGA
56console.log(s.toLowerCase()); // abcdefga
57
58console.log(s[0]); // a
59console.log(s[1]); // b
60console.log(s.charAt(0)); // a
61console.log(s.charAt(1)); // b
62
63// pad spaces to the left to make the length 3
64console.log('a' +
65 'x'.padStart(3)) // "a x"
66
67console.log('a' +
68 'x'.padStart(3, '-')) // "a--x"
69
70// pad with '-' to the left to make the total length 5
71console.log('a' +
72 'xy'.padStart(5, '-')) // "a---xy"
73
74console.log('a' +
75 ' b '.trim() + 'c'); // abc
76
77console.log('a' +
78 ' b '.trimStart() + 'c'); // ab c
79
80console.log('a' +
81 ' b '.trimEnd() + 'c'); // a bc
82
83console.log('ab'.repeat(3)); // ababab
84
85console.log(String(123) === "123"); // true, a number to a string
86
87let k = 20
88console.log(k.toString() === "20"); // true
89console.log("0x" + k.toString(16)); // 0x14, hexadecimal
90
91k = 12.3456;
92console.log(k.toFixed(0)); // 12
93
94k = 12.5;
95console.log(k.toFixed(0)); // 13, note that it performs rounding
96
97k = 12.3456;
98console.log(k.toFixed(1)); // 12.3
99console.log(k.toFixed(2)); // 12.35, note that it performs rounding
100console.log(k.toFixed(5)); // 12.34560
101
102console.log(k.toExponential(1)); // 1.2e+1
103console.log(k.toExponential(2)); // 1.23e+1
104console.log(k.toExponential(3)); // 1.235e+1
105
106console.log(k.toPrecision(1)); // 1e+1
107console.log(k.toPrecision(2)); // 12
108console.log(k.toPrecision(3)); // 12.3
109console.log(k.toPrecision(4)); // 12.35
110console.log(k.toPrecision(5)); // 12.346
String interpolation example:
const myName = "world";
console.log(`hello ${myName}`);