-
Notifications
You must be signed in to change notification settings - Fork 1
/
Substring1.js
49 lines (30 loc) · 1.24 KB
/
Substring1.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
// String Methods Implementation
// Searching for Sub String
//str.indexOf(substr, pos)
// It looks for the substr in str, starting from the given position pos, and returns the position where the match was found or -1 if nothing can be found.
let string1 = 'Prasanna loves JavaScript';
console.log(string1.indexOf('loves'));
console.log(string1.indexOf('loves', 2));
// Finding all occurances using a loop
let string2 = 'As sly as a fox, as strong as an ox';
let target = 'as'; // let's look for it
let pos = 0;
while (true) {
let foundPosition = string2.indexOf(target, pos);
if (foundPosition == -1) break;
console.log(`Found at ${foundPosition}`);
pos = foundPosition + 1;
}
// Searching from the end
let string3 = 'Prasanna loves JavaScript';
console.log(string3.lastIndexOf('loves'));
// IMP: We can’t put it in the if like this:
let string4 = 'Prasanna loves JavaScript';
if (string4.indexOf('Prasanna')) {
console.log('The Sub-String Prasanna is found!');
}
// str.indexOf("Widget") returns 0 (meaning that it found the match at the starting position). Right, but if considers 0 to be false.
if (string4.indexOf('Prasanna') != -1) {
console.log('Yey! the Sub-string Prasanna is found');
}
// IMP : if (~str.indexOf(...)) = if found