-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path24.string_methods_deep_dive.js
More file actions
286 lines (221 loc) · 6.19 KB
/
Copy path24.string_methods_deep_dive.js
File metadata and controls
286 lines (221 loc) · 6.19 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/********************************************************
* Topic: String Methods Deep Dive
* Author: Ashutosh (JS Interview Prep)
********************************************************/
/*
========================================================
0️⃣ Most Asked String Methods (MEMORIZE)
========================================================
length
toUpperCase
toLowerCase
trim
slice
substring
replace
replaceAll
split
includes
startsWith
endsWith
charAt
indexOf
repeat
template literals
*/
/*
========================================================
1️⃣ length
========================================================
*/
const str = "JavaScript";
console.log(str.length); // 10
/*
========================================================
2️⃣ Case Methods
========================================================
*/
const name = "ashutosh";
console.log(name.toUpperCase()); // ASHUTOSH
console.log(name.toLowerCase()); // ashutosh
/*
========================================================
3️⃣ trim()
========================================================
Real use: form inputs
*/
const input = " hello ";
console.log(input.trim()); // "hello"
console.log(input.trimStart()); // left trim
console.log(input.trimEnd()); // right trim
/*
========================================================
4️⃣ slice() (VERY IMPORTANT 🔥)
========================================================
Supports negative index
*/
const text = "JavaScript";
console.log(text.slice(0,4)); // Java
console.log(text.slice(-6)); // Script
/*
slice vs substring
*/
console.log(text.substring(0,4)); // Java
// substring doesn't support negative index
/*
========================================================
5️⃣ replace & replaceAll
========================================================
*/
let msg = "Hello world world";
console.log(msg.replace("world","Ashu"));
// replaces first only
console.log(msg.replaceAll("world","JS"));
// replaces all
/*
========================================================
6️⃣ split() (MOST IMPORTANT 🔥)
========================================================
*/
let data = "a,b,c,d";
let arr = data.split(",");
console.log(arr); // ["a","b","c","d"]
/*
========================================================
7️⃣ includes / startsWith / endsWith
========================================================
*/
let sentence = "I love JavaScript";
console.log(sentence.includes("love")); // true
console.log(sentence.startsWith("I")); // true
console.log(sentence.endsWith("Script")); // true
/*
========================================================
8️⃣ charAt & indexOf
========================================================
*/
let s = "hello";
console.log(s.charAt(1)); // e
console.log(s.indexOf("l")); // 2
/*
========================================================
9️⃣ repeat()
========================================================
*/
console.log("ha".repeat(3)); // hahaha
/*
========================================================
🔟 Template Literals
========================================================
*/
let user = "Ashu";
console.log(`Hello ${user}`);
/*
========================================================
1️⃣1️⃣ Reverse String (INTERVIEW FAV 🔥)
========================================================
*/
function reverseString(str){
return str.split("").reverse().join("");
}
console.log(reverseString("hello"));
/*
========================================================
1️⃣2️⃣ Palindrome Check
========================================================
*/
function isPalindrome(str){
let rev = str.split("").reverse().join("");
return str === rev;
}
console.log(isPalindrome("madam")); // true
console.log(isPalindrome("hello")); // false
/*
========================================================
1️⃣3️⃣ Count Characters
========================================================
*/
function charCount(str){
let map = {};
for(let ch of str){
map[ch] = (map[ch] || 0) + 1;
}
return map;
}
console.log(charCount("banana"));
/*
========================================================
1️⃣4️⃣ Count Vowels
========================================================
*/
function countVowels(str){
let count = 0;
let vowels = "aeiou";
for(let ch of str.toLowerCase()){
if(vowels.includes(ch)){
count++;
}
}
return count;
}
console.log(countVowels("JavaScript"));
/*
========================================================
1️⃣5️⃣ Remove Duplicates
========================================================
*/
function removeDuplicates(str){
return [...new Set(str)].join("");
}
console.log(removeDuplicates("programming"));
/*
========================================================
1️⃣6️⃣ Reverse Words
========================================================
*/
function reverseWords(str){
return str.split(" ").reverse().join(" ");
}
console.log(reverseWords("I love JS"));
/*
========================================================
1️⃣7️⃣ String Coercion Tricks
========================================================
*/
console.log("5" + 2); // "52"
console.log("5" - 2); // 3
/*
========================================================
1️⃣8️⃣ Output Based Questions
========================================================
*/
console.log("hello".slice(1,3)); // el
console.log("hello".substring(1,3)); // el
console.log("5" + 1); // "51"
console.log("5" - 1); // 4
/*
========================================================
1️⃣9️⃣ Important Differences
========================================================
slice → negative index allowed
substring → negative not allowed
replace → first only
replaceAll → all
split → returns array
*/
/*
========================================================
🔥 Interview One-Liners
========================================================
✔ Strings immutable
✔ split returns array
✔ slice supports negative
✔ replace vs replaceAll
✔ template literals modern way
✔ reverse string using split reverse join
*/
/*
========================================================
🔥 END OF FILE: string_methods_deep_dive.js
========================================================
*/