-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings.js
More file actions
425 lines (268 loc) · 10.4 KB
/
Copy pathstrings.js
File metadata and controls
425 lines (268 loc) · 10.4 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
"use strict";
// lets assume an example of an airline in order to demonstrate strings
const airline = "Fly Emirates";
const plane = "F320";
// Just Like Arrays, Strings also have indexes:
// And it starts from 0
// And using indexes we can get any character of string
console.log(plane[0]);
console.log(airline[4]);
// So just like an array we can get the character of string at a certain position
// and same of course for any letter:
console.log(plane[1]);
// but the characters that are retrieved are still strings
// And we can do the same on string directly
console.log("Haji"[0]);
// Reading Length of String:
console.log("haji".length);
console.log(plane.length);
console.log(airline.length);
// ============
// String Methods:
// Strings also have methods and some of them are very similar to arrays
// IndexOf():
// used to get the position in which a certain letter is in the string
console.log("haji".indexOf("j"));
console.log(airline.indexOf("E"));
// Strings are also 0 based
// And of-course the space here also count as character
// Now it gives us the index of first occurrence
// But sometime we might need the last one so for that we have:
// lastIndexOf()
console.log(airline.lastIndexOf("e"));
console.log("haji newton".indexOf("n"));
console.log("haji newton".lastIndexOf("n"));
// So thats the difference
// And we can also search for an entire word
console.log(airline.indexOf("Emirates"));
// this one is actually case sensitive
console.log(airline.indexOf("emirates"));
// if case does not matches it return -1
// because this can now not be found in the airline string
//? Where do we use the indexes?
// !To extract some part of a string
// using slice()
// The slice method needs indexes as arguments
// Arguments of Slice():
// Start Index
// End Index
airline;
plane;
console.log(airline.indexOf("E"));
// Again its case sensitive
console.log(airline.slice(4));
// So it starts extracting from Index of E thats what figured out above
// ! And the result we got here is called substring
// because its just a part of string
// here we only passed the begin parameter
// so it extracted everything all the way out
// Now this does not change the original string
airline;
// And it is because its impossible to mutate strings as they are primitives
//! And the slice() return the extracted substring
// And so we can store it in a variable
const stadium = "Qaddafi Cricket Stadium Lahore";
// And beside the begin parameter we also have the end parameter
// End parameter: Index Number
const st = stadium.slice(stadium.indexOf("Cricket"), stadium.lastIndexOf(" "));
st;
//! So basically it stops extracting before reaching the end parameter index
// So this is how we make use of indexes
airline;
// the length of the string will always be end parameter minus start
console.log(airline.indexOf("E"));
console.log(airline.slice(4, 7));
console.log(airline.slice(4, 7).length);
// 7 - 4 = 3
// So:
// indexOf()
// lastIndexOf()
// are very useful
// of-course we can hard code these values but sometime we don't know these indexes
// So let say we want to extract the first word out of a string
stadium;
const first = stadium.slice(0, stadium.indexOf(" ")); // 0 to 1st space occurrence
first;
// so we got it with begin param 0 and end to first space index
// lets extract "Lahore";
const lhr = stadium.slice(stadium.indexOf("L"));
const lhr2 = stadium.slice(stadium.lastIndexOf(" ") + 1); //added one to remove index of space
lhr;
lhr2;
// we can do it both ways
// So thats fundamentals of slice
// But we can do even more with it:
// We can even define a negative begin parameter
airline;
stadium;
lhr;
first;
console.log(first.slice(-2));
// by passing negative begin parameter it will start counting indexes from end and extract it
// we can also specify a negative end parameter
console.log(airline.slice(airline.indexOf(" "), -2));
// So starting from 1st space to the end, and it will skip last 2 elements from the end
// Now lets do some practice with real world looking examples:
// Lets create a function that receives a string : seat number and logs if it is middle seat or not
// Let suppose if it contains B or E then it is a middle seat
// So basically all we want to do it check if the received string contains B or E
const isMiddleSeat = function (seat) {
const s = seat.slice(-1);
if (s === "B" || s === "E") {
console.log("You got middle seat");
} else {
console.log("You got luck man :)");
}
};
isMiddleSeat("11B");
isMiddleSeat("21E");
isMiddleSeat("12C");
// But why all of this work?
// So we know strings are just primitives
// Then how do they got methods
// Because we know methods only belongs to objects
// Well that is actually true
//! However JavaScript is really smart
//! Primitives are not objects, so technically they shouldn’t have methods.
//! However, JavaScript does something smart under the hood — it temporarily wraps the primitive in a special object called a wrapper object.
// ? So this is how it works:
//? Whenever we call a method on a string JavaScript will automatically behind the scenes convert that string primitive to string Object with the same content and then its on that object where the methods are called.
//! And this process is called Boxing
// Because it basically takes our string and puts it into a box which is the object
// And basically JavaScript calls the String Constructor
console.log(typeof new String("haji"));
// So this is the conversion that JavaScript does behind the scenes whenever we call a method on string
//! And then, when the operation is done, the object is converted back to regular string
// ! And In fact all the string methods return a primitive even if they are called on a string object
lhr;
lhr2;
first;
console.log(typeof lhr);
console.log(typeof lhr2);
console.log(typeof first);
// ========
// Lets Continue with some more string methods
// toLowerCase();
console.log("HAJI".toLocaleLowerCase());
// toUpperCase();
console.log("haji".toLocaleUpperCase());
// And these methods require no arguments
// fix capitalization in a name
const passenger = "haJI";
const passengerLower = passenger.toLocaleLowerCase();
passengerLower;
const fixed = passengerLower[0].toUpperCase() + passengerLower.slice(1);
fixed;
// can create a function for this purpose
const capitalize = function (pName) {
const lower = pName.toLowerCase();
return lower[0].toLocaleUpperCase() + lower.slice(1);
};
console.log(capitalize("jon"));
console.log(capitalize("jOn"));
console.log(capitalize("riCky"));
// Lets create a function to check user input email:
const email = "hello@toqir.io";
// but now as the user logs in accidentally they do it all wrong
const loginEmail = "Hello@Toqir.io";
// but still this email here is kind of correct and valid And so we can now still compare if these two are same
//! Now when we check the user input:
// Steps :
// convert everything to lowerCase:
const lowerEmail = loginEmail.toLocaleLowerCase();
// get rid of all the white spaces, so
// empty spaces
// also the enter button pressed, that is also counted as a space
// And to remove white spaces we use trim()
const trimmedEmail = lowerEmail.trim();
trimmedEmail;
loginEmail;
email;
// so now we can compare trimmed email and user's real email
console.log(email === trimmedEmail);
// and we can even do it in a better way
const normalizedEmail = loginEmail.toLocaleLowerCase().trim();
normalizedEmail;
// thats so cool
// so we chained multiple methods together in order to implement it
// and now again we can compare it
console.log(email === normalizedEmail);
// And Since ES 2019 there are also:
// trimStart()
// trimEnd()
// next up lets learn about one of the most important things about strings
// which is to replace parts of strings
// So let say
// const priceGB = "288,97€";
const priceGB = "288,97£";
// flight price for Great Britain
// ! In Europe comma (,) is used as decimal separator
// ! In US period (.) is used as decimal separator
// Now we want to compute the price ein USD
// For that we need to replace the £ with $ and comma with period
// For that we have
//! replace(x,y)
// x : value that we want to replace
// y : string that will replace the first one
// returns: a new string
const priceUSD = priceGB.replace("£", "$").replace(",", ".");
priceUSD;
// thats soo cool indeed
// And we can also replace the entire words not just single characters
// Let say
const announcement = "All passengers please come to the boarding door 23";
// However usually it is called gate not door
// So lets create
console.log(announcement.replace("door", "gate"));
// So this replace only replace the first occurrence
const announcement2 =
"All passengers please come to the boarding door 23. Please make sure you are at the door before 5 minutes of take of";
console.log(announcement2.replace("door", "gate"));
// And the solution to this is replaceAll()
console.log(announcement2.replaceAll("door", "gate"));
// ! replace method is also case sensitive
// There are also 3 methods that return booleans:
//includes()
plane;
console.log(plane.includes("F3"));
console.log(plane.includes("a"));
console.log(plane.includes("f"));
console.log(plane.includes("F"));
// this is also case sensitive
// startsWith()
airline;
plane;
email;
console.log(plane.startsWith("f"));
console.log(plane.startsWith("F"));
console.log(email.startsWith("hel"));
console.log(email.startsWith("H"));
// endsWith();
console.log(email.endsWith(".io"));
console.log(plane.endsWith("0"));
// split();
// allows to split a string with multiple parts based on a divider string
// returns an array
console.log("haji".split("")); //divider: empty string
console.log("A+very+nice+day".split("+")); //divider: +
console.log("A+very+nice+day".split(""));
// lets make a good use of it:
const haji = "Haji Jones";
console.log(haji.split(" "));
const [firstName, lastName] = haji.split(" ");
firstName;
lastName;
// Join();
// Opposite of split
// joins elements of an array to string
console.log([1, 2, 3, 4, 5].join(""));
console.log([1, 2, 3, 4, 5].join(" "));
console.log([1, 2, 3, 4, 5].join("-"));
console.log("haji".split(""));
// ! Split is a string method and Join is an Array method
// repeat();
// allows us to repeat the string multiple times
// accepts a number as an argument
// that decides how many time it will be repeated
console.log(" haji".repeat(5));
// ! And there are many more string methods of-course, may refer to MDN Web DOCS