-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.js
286 lines (238 loc) · 10.1 KB
/
string.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
//! for emoji use "window + ."
//! String in JavaScript
//! Length of string : str.length;
//length of a string is the number of character it contains,
//including spaces, punctuation, and control characters.
// const str="kriti yadav \n yes";
// console.log(str);
//! Escape Character
//In javascript "\"used as escape character
// like: \' , \" ,\\
//Example 1:
//console.log(" \'kriti\' how are you do you \"suffering\" \n listen you're not this weak now you are young your maa is old \n lady yet handling everything \n so gracefully so how could you lie down at \nfront of such problem which can easily solve focus on upcoming life \n there you have to available bcz she has that expectation which keep her moving");
//!String search Methods
//? a. indexOf(): The indexOf() method returns the index (position)
//of the first occurrence of a string,or return -1 if the string is not found.
//?syntax
//indexOf(searchString);
//indexOf(searchString, position);
//*Example 1:
// const str="Thapa Technical";
// console.log(str.indexOf("ech"));
//!Example 2: (Extra)
// const str="Thapa Technical"
// let strArray=Array.from(str);
// console.log(strArray);
//? get element with index through map
// let strMap=strArray.map((ele,index)=>{
// return `${ele}-${index}`;
// })
// console.log(strMap);
//? b. lastIndexOf(): The lastIndex() method returns the index of the last
//occurrence of a specific text in a string.
//? Syntax
// lastIndexOf(searchString)
// lastIndexOf(searchString ,position)(position specify from zero to position you have range to find)
//!Example 1:
// let text="hello guys,guys welcome hows life workout and made change"
// console.log(text.lastIndexOf("guys",10));
//? c. search():The search() method searches a string for a
//(-1)
//string (or a regular expression) and return the position of the match.
//(return first index where match found else return -1).
//!Example 2:
// let text="Hello People, lala looking forward Hello for more";
// const result=text.search(/la/);
// console.log(result);
// console.log(text.search("Hello"));
// console.log(text.search(/He/));
// console.log(text.search(/hello/));
// console.log(text.search(/Hello/g));//*( g represent global search but always consider first index)
// console.log(text.search(/hello/i));//!(i represents ignore case-sensitivity)
// * Important Tips:
//! In search you cannot write position
//! match(): Return an array of the matched value or null if no match is found.
//null (case sensitive)
// let text="Hello People, lala looking forward Hello for more";
// let result =text.match(/hello/i);
// console.log(result);
// let result1=text.match(/hello/ig);//i: ignore, g:global here you can see use of g bcz here by including this U get element search else than array
// console.log(result1);
//! matchAll():Returns an iterator of all matches, providing detailed information about each match .Return an empty iterator if no match is found.
// {} (not case sensitive)
// let text="Hello People, lala looking forward Hello for more";
// let matchResult=text.matchAll("Hello");
// 1. console.log(...matchResult);
// 2. for ( let item of matchResult){
// console.log(item);
// }
//*Both 1 & 2 are same !!
//! key Other ways
// * Only to get element value we use iterator
// for (let item of matchResult) {
// console.log(item[0]);
// }
// for(let index of matchResult){
// console.log(index.index);
// }
// for(let {input} of matchResult){
// console.log(input);
// }
//! includes(): Returns true if string contains the specific value, else false.
// case sensitive //came in ES6 feature
// we can'nt take regular expression over here like (/hey/i)":("
// let text="Hello People, lala looking forward Hello for more";
// console.log(text.includes("HelLi"));
//! startsWith(): The startsWith() return true if string begin with specified value else return false;
//case sensitive
// let text="Hello People, lala looking forward Hello for more";
// console.log(text.startsWith("Hello People"));
// //* specify start position
// console.log(text.startsWith("lala",14));
//! endsWith(): The endsWith() method return true if string end with specified value ,else false;
// console.log(text.endsWith(" more"));
//! Extracting string Parts:
//? String.prototype.substr() it is deprecated ❌
//? a. slice() extracts a part of a string and return the extracted part in a new string.
//* syntax:
//slice(start,end) (last end index don'nt included, start index from 0)
//* example 1:
// let text="Hello People, lala looking forward Hello for more";
// console.log(text.slice(4));
// console.log(text.slice(7,25));
//? b. substring() extracts a part of string based on starting and ending index.
//camelCase used to separate words,substring is not intended as Sub String but as Substring.
//*syntax:
// substring(indexStart) //index start with 0
// subString(indexStart, indexEnd);
//? substring() is similar to slice().The difference is that start and end values less than 0 in
//? substring treated as 0,But in slice() work as negative index and then took start point
// let text="Hello People, lala looking forward Hello for more";
// console.log(text.slice(-4));
// console.log(text.substring(-4));
//! Similarity
//todo In both the slice() and substring() methods,the end parameter indicates the ending index up to which the extraction occur, but the character at the end index is excluded from substring.
//? interview Questions:
//let text="Hello People, lala looking forward Hello for more";
//let result = text.slice(1);
//let result=text.replace("H","");//(first exist H will only replace)
//let result=text.substring(1);
//console.log(result);(all three give same result)
//! Key concept
//let result=text.replace("Hello","lovely"); // remove first exist element
//let result=text.replaceAll("Hello","Vinod"); //replace all existing element
//console.log(result);
//! 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.
// negative index not consider
//let text="Hello People, lala looking forward Hello for more";
// let result=text.charAt(6);
// let result=text.charAt(-6); //{}mean blank space we get
//console.log(result);
//*charCodeAt(): The charCodeAt() method returns the code of the
// negative index not consider
//character at a specified index in a string. The method returns a
//UTF-16 (an integer between 0 and 65535).
//let text="Hello People, lala looking forward Hello for more";
// let result=text.charCodeAt(6);
//let result=text.charAt(-6); //{}mean blank space we get
//console.log(result);
//todo ES2022 introduced the string method at():
//? The at() method returns the character at a specified index (position).same as charAt()
//negative index also work here
//let text="Hello People, lala looking forward Hello for more";
//let result=text.at(6);
//let result=text.at(-6); //{}mean blank space we get
//console.log(result);
//! Replacing String Content:
//? replace(): The replace method is used to replace a specified value with another value.
//* Example:1
// const str="Hello,World!";
//const newStr =str.replace("World","Vinod");
// const newStr =str.replace(/World/,"Vinod");
// console.log(newStr);
//* Example:2
// let text="Hello People, lala looking forward Hello for more";
// const newStr =text.replace(/hello/ig,"Vinod");
// console.log(newStr);
//! Other useful Method:
//* toUpperCase()
// let text="Hello People, lala looking forward Hello for more";
// console.log(text.toUpperCase());
//*toLowerCase()
// console.log(text.toLowerCase());
//* trim:Removes whitespace from both ends of the string
// Example 1:
// 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";
// 1.let strArr = str.split(",");
//? 2. let strArr = str.split(",").reverse();**
// 3. let strArr= str.split("").reverse();
// 4. let strArr= str.split(",").reverse().join(); //(array to string again through join())
// 5. console.log(strArr);
//! Interview Question :
//! Q1 Write a JavaScript function that print the letter 'a' through 'z' in the console. You should
// use a loop to iterate the letters and print each one on a new line?
console.log("a".charCodeAt(0));//97
console.log("z".charCodeAt(0));//122
//! to convert code to char : fromCharCode(char) (we putting string bcz there it exist)
for(let char=97;char<123;char++){
console.log(String.fromCharCode(char));
}
//! Q2 Write a function to check if all the vowels presents in a string or not ?
let items="my name u is vinod @ thapa";
const checkAllVowelPresentOrNot=(str)=>{
const vowel="aeiou";
for(let char of vowel){
if(!str.includes(char)){
return false;
}
}
return true;
}
console.log(checkAllVowelPresentOrNot(items));
//! Q3 Count number of vowel in the string.
const countVowel=(str)=>{
const vowel="aeiou";
let count=0;
for(let char of str){
if(vowel.includes(char)){
count++;
}
}
return count;
}
console.log(countVowel("Hello a i o u World!"));
//! Q4 check string is pangram(all a2z char present)
//* Attempt 1:
// const pangramChecker=(str)=>{
// let totalAlpha="";
// for(let char =97;char<=122;char++){
// totalAlpha = totalAlpha + String.fromCharCode(char);
// }
// console.log(totalAlpha);
// for(let char of totalAlpha){
// if(!str.includes(char))
// return false;
// }
// return true;
// };
//* Attempt 2:
// const pangramChecker=(str)=>{
// let inputArr= str.toLowerCase().split("");
// const values =inputArr.filter((curr)=>
// curr.charCodeAt()>="a".charCodeAt() &&
// curr.charCodeAt()<="z".charCodeAt()
// );
// return [...new Set(values)].length===26; ( or new Set(values).size ===26 )
// };
console.log(pangramChecker("The quick brown fox jumps over the lazy dog"));