-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7_strings.js
329 lines (253 loc) · 12 KB
/
7_strings.js
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
//* =========================================
//* String in JavaScript
//* =========================================
//? Strings in JavaScript are a fundamental data type that represents a sequence of characters.
// Note:
//? Strings created with single or double quotes works the same.
// There is no difference between the two.
//* String Properties:
//? length: Property that returns the length of the string (number of characters).
// const str = "Hello, World!";
// console.log(str.length);
// including space n all
//* =========================================
//* Escape Character
//* =========================================
//? Escape Character: In JavaScript, the backslash \ is used as an escape character. It allows you to include special characters in a string.
// Code Result Description
// \' ' Single quote
// \" " Double quote
// \\ \ Backslash
// let text = "My name is "Vanshika Thesiya " & I am a Full Stack Developer. ";
// let text =
// "My name is ' Vanshika Thesiya ' & \\ I am a \"Full Stack \" Developer. ";
// // let text = 'My name is " Vanshika Thesiya " & I am a Full Stack Developer. ';
// console.log(text);
//* =========================================
//* String Search Methods
//* =========================================
//? 2: String Search Methods
//? a: indexOf(): The indexOf() method returns the index (position) of the first occurrence of a string in a string, or it returns -1 if the string is not found:
// syntax
// indexOf(searchString)
// indexOf(searchString, position)
// let text = "Vanshika Thesiya";
// console.log(text.indexOf("Thesiya"));
// The indexOf() method is case sensitive.
// console.log(text.indexOf("Thesiya"));
// let strArr = Array.from(text);
// // console.log(strArr);
// let strMap = strArr.map((curElem, index) => `${curElem} - ${index}`);
// console.log(strMap);
//? b: lastIndexOf() : The lastIndexOf() method returns the index of the last occurrence of a specified text in a string:
// syntax
// lastIndexOf(searchString)
// lastIndexOf(searchString, position)
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
// let index = text.indexOf("JavaScript");
// let index = text.lastIndexOf("JavaScript");
// let index = text.lastIndexOf("JavaScript", 40);
// console.log(index);
//? c: search(): The search() method searches a string for a string (or a regular expression) and returns the position of the match.
//* Returns the index number where the first match is found. Returns -1 if no match is found.
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
// let result = text.search(/Javascript/i);
// console.log(result);
//*👉 Important Tips
// The search() method cannot take a second start position argument.
// The indexOf() method cannot take powerful search values (regular expressions).
// They accept the same arguments (parameters), and return the same value
//? match() : Returns an array of the matched values or null if no match is found.
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
// let result = text.match("Javascript");
// let result = text.match("JavaScript");
//todo here the js converts the normal text into regular expression text.match(/JavaScript/); without the g flag
// let result = text.match(/Javascript/gi);
// console.log(result);
//? matchAll() : Returns an iterator of all matches, providing detailed information about each match. Returns an empty iterator if no match is found.
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
// let matchResult = text.matchAll("javascript");
// let matchResult = text.matchAll("JavaScript");
//todo here the js converts the normal text into regular expression text.match(/JavaScript/g); also adds the g flag at the end
// console.log(...matchResult);
// for (let item of matchResult) {
// console.log(item[0]);
// }
// for (let index of matchResult) {
// console.log(index.index);
// }
// for (let { index } of matchResult) {
// console.log(index);
// }
//? includes(): Returns true if the string contains the specified value, and false otherwise.
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
// let includeResult = text.includes(/java/i);
// let includeResult = text.includes("J");
// console.log(includeResult);
// Note: includes() is case sensitive. includes() is an ES6 feature.
//? startsWith(): The startsWith() method returns true if a string begins with a specified value.Otherwise it returns false:
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
// let result = text.startsWith("Helcome");
// let result = text.startsWith("Hello");
// console.log(result);
//* start position for the search can be specified
// let result = text.startsWith("welcome", 18);
// let result = text.startsWith("welcome", 17);
// console.log(result);
//? endsWith(): The endsWith() method returns true if a string ends with a specified value. Otherwise it returns false:
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
// let result = text.endsWith("welcome");
// let result = text.endsWith("course");
// console.log(result);
//* =========================================
//* Extracting String Parts:
//* =========================================
//! Extracting String Parts:
//? String.prototype.substr() it is deprecated ❌
//? a: slice() extracts a part of a string and returns the extracted part in a new string.
// syntax
// slice(start, end);
// Todo JavaScript counts positions from zero.
//? First position is 0. Second position is 1.
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
// let result = text.slice(6);
// let result = text.slice(6, 15);
// console.log(result);
// subString() substring()
//? a: substring: Extracts a portion of the string based on starting and ending indices.
//* camelCase is used to separate words, substring is not to be intended as Sub String but as Substring
// syntax
// substring(indexStart) // index starts with 0
// substring(indexStart, indexEnd)
//* substring() is similar to slice(). The difference is that start and end values less than 0 are treated as 0 in substring().
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
// let result = text.slice(-6);
// console.log(result);
//! Homework
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
// let result = text.substring(0);
// let result = text.substring(1);
// let result = text.substring(-5);
// console.log(result);
//! similarities
//todo In both the slice() and substring() methods, the end parameter indicates the ending index up to which the extraction occurs, but the character at the end index is excluded from the extracted substring.
//* =========================================
//* Interview Question
//* =========================================
//! What is the output for the following code?
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
// let result = text.slice(1);
// let result = text.replace("H", "");
// let result = text.substring(1);
//? Optional
// let result = text.replace("JavaScript", "Vinod");
// let result = text.replaceAll("JavaScript", "Vinod");
// console.log(result);
//* =========================================
//* Extracting String Characters
//* =========================================
//! Extracting String Characters
// There are 3 methods for extracting string characters:
//? The charAt(position) Method
//? The charCodeAt(position) Method
//? The at(position) Method
//? charAT() : The charAt() method returns the character at a specified index (position) in a string
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
// let result = text.charAt(6);
// let result = text.charAt(-6);
// console.log(result);
//? charCodeAt() : The charCodeAt() method returns the code of the character at a specified index in a string. The method returns a UTF-16 code (an integer between 0 and 65535).
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
// let result = text.charCodeAt(6);
// console.log(result);
//todo ES2022 introduced the string method at():
//? The at() method returns the character at a specified index (position) in a string. The at() method returns the same as carAt().
// let text = "Hello JavaScript, welcome to our world best JavaScript course";
// let result = text.at(-6);
// console.log(result);
//todo Note
// The at() method is a new addition to JavaScript.
// It allows the use of negative indexes while charAt() do not.
// Now you can use myString.at(-2) instead of charAt(myString.length-2).
//* =========================================
//* Replacing String Content:
//* =========================================
//! Replacing String Content:
//? replace() : The replace method is used to replace a specified value with another value.
// const str = "Hello, World!";
// const newStr = str.replace("World", "JavaScript");
// console.log(newStr); // Outputs: Hello, JavaScript!
//? Case-Insensitive Replacement: To perform a case-insensitive replacement, you can use the i flag in the regular expression.
// let originalString = "Hello, World! How are you, World?";
// let replacedString = originalString.replace(/world/gi, "vinod");
// console.log(replacedString);
//* =========================================
//* Other Useful Methods:
//* =========================================
//! Other Useful Methods:
//? toUpperCase and toLowerCase: Converts the string to uppercase or lowercase.
// const str = "JavaScript";
// console.log(str.toUpperCase()); // Outputs: JAVASCRIPT
// console.log(str.toLowerCase()); // Outputs: javascript
//? trim: Removes whitespace from both ends of the string.
// const str = " Hello, World! ";
// console.log(str.length);
// let trimStr = str.trim();
// console.log(trimStr);
// console.log(trimStr.length);
//? split: Splits the string into an array of substrings based on a specified delimiter.
// const str = "apple,orange,banana";
// let strArr = str.split(",").reverse().join();
// console.log(strArr);
//* =========================================
//* //! Interview Questions
//* =========================================
//! 1: Write a JavaScript function that prints the letters 'a' through 'z' in the console. You should use a loop to iterate through the letters and print each one on a new line.
// console.log("a".charCodeAt(0));
// console.log("z".charCodeAt(0));
// for (let char = 97; char <= 122; char++) {
// console.log(String.fromCharCode(char));
// }
//! 2: Write a function to count the number of vowels in a string?
// const countVowels = (str) => {
// const vowels = "aeiou";
// let count = 0;
// for (let char of str) {
// console.log(char);
// // console.log(str.includes(char));
// if (vowels.includes(char)) {
// count++;
// }
// }
// return count;
// };
// console.log(checkAllVowelPresentOrNot("my name u is vinod @ thapa"));
// console.log(countVowels("Hello a i o u world"));
//! 3: Write a function to check if all the vowels presents in a string or not?
// const checkAllVowelPresentOrNot = (str) => {
// const vowels = "aeiou";
// for (let char of vowels) {
// // console.log(char);
// // console.log(str.includes(char));
// if (!str.includes(char)) {
// return false;
// }
// }
// return true;
// };
// console.log(checkAllVowelPresentOrNot("my name u is vinod @ thapa"));
//! 4: Write a JavaScript function to check if the given sting is Pangram or not?
// const pangramChecker = (str) => {
// let inputArr = str.toLowerCase().split("");
// // console.log(inputArr);
// // // console.log("z".charCodeAt());
// const values = inputArr.filter(
// (curElem) =>
// curElem.charCodeAt() >= "a".charCodeAt() &&
// curElem.charCodeAt() <= "z".charCodeAt()
// );
// // console.log(values);
// return new Set(values).size === 26;
// // return [...new Set(values)].length === 26;
// };
// console.log(pangramChecker("The quick @ brown fox jumps ove the lazy dog"));