-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCH6_String.js
More file actions
133 lines (75 loc) · 2.14 KB
/
CH6_String.js
File metadata and controls
133 lines (75 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
let str = "Any String";
let str3 = "Any String";
// console.log(str === str3); // true
// console.log(str);
let str2 = new String("Any String");
// console.log(str === str2); // true
// Escape Characters
// console.log('Hello\nWorld'); // Hello
// // World
// console.log('Hello\tWorld'); // Hello\tWorld
// console.log('Hello\\World'); // Hello\tWorld
// String Template
let name = 'Sani'
// console.log(`Hello, ${name}!`);
// String Method
// length
// let a = "This is a string"
// console.log(a.length)
// charAt()
// console.log(a.charAt(3))
// charCodeAt
// return ascii value
// console.log(a.charCodeAt(3))
// at()
// console.log(a.at(2));
let b = "This is a string"
// String[]
// console.log(b[6]);
// slice
// let slicePart = b.slice(1, 4) //last value is excluded
let slicePart = b.slice(-4) //last value is excluded
// console.log(slicePart);
// split
let splitPart = "my, name, is, sani".split(',', 3) // limit is 3 that is the length of the result array
console.log(splitPart);
// Substring
let substringPart = b.substring(-1, 4) // does not take negative index
// console.log(substringPart);
// subStr
let substrPart = b.substr(1, 5) //length is 4
// console.log(substrPart);
// uppercase
console.log("small".toUpperCase());
// lowercase
console.log("BIG".toLowerCase());
// string concat
let p = "one "
let q = "two "
// let concat = p + q
// console.log(concat);
console.log(p.concat(q));
// trip
let s = " hello world "
console.log(s.trim());
console.log(s.trimStart())
console.log(s.trimEnd())
// search()
console.log(s.search("hello")) // returns index of first occurrence
console.log(s.search("WORLD")) // returns -1 if not found
// padStart()
let str4 = "123"
console.log(str4.padStart(5, '@')) // first argument is length
console.log(str4.padEnd(5, '@')) // first argument is length
// repeat
console.log("repeat ".repeat(5));
// replace
let str5 = "Hello, World!"
console.log(str5.replace("World", "Sani"))
// match
let str6 = "Hello, World! Hello, Sani!"
console.log(str6.match("Sani"))
// string loop
for (let i = 0; i < str6.length; i++) {
console.log(str6[i]);
}