Skip to content

Commit 118c216

Browse files
author
Wakidur Rahaman
committed
String Search
1 parent d1a04ac commit 118c216

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/exercises/string/exercise-01.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,30 @@ const c = 'add';
2525
const d = 'ab';
2626

2727
console.log(a < b); // Prints 'false'
28+
29+
// String Search
30+
31+
'Red Dragon'.indexOf('Red'); // returns 0;
32+
'Red Dragon'.indexOf('RedScale'); // returns -1
33+
'Red Dragon'.indexOf('Dragon', 0); // returns 4
34+
35+
function existsInString(stringValue: string, search: string) {
36+
return stringValue.indexOf(search) !== -1;
37+
}
38+
39+
console.log(existsInString('red', 'r')); // prints 'true';
40+
console.log(existsInString('red', 'b')); // prints 'false';
41+
42+
const stringLong = 'He is my king from this day until his last day';
43+
let count = 0;
44+
let pos = stringLong.indexOf('a');
45+
46+
while (pos !== -1) {
47+
count++;
48+
pos = stringLong.indexOf('a', pos + 1);
49+
}
50+
51+
console.log(count);
52+
53+
'Red Dragon'.startsWith('Red'); // Returns true
54+
'Red Dragon'.endsWith('Dragon'); // Returns true

0 commit comments

Comments
 (0)