Skip to content

Commit 6c36c35

Browse files
Merge pull request #63 from wakidurrahman/feat/string-01
JavaScript String Primitive
2 parents 2b175fe + 118c216 commit 6c36c35

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

src/exercises/string/exercise-01.ts

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,54 @@
1-
let a = 'a';
2-
let b = 'b';
1+
/**
2+
* JavaScript String Primitive
3+
* JavaScript's native String primitive comes with various common string functions.
4+
*
5+
* 1. String Access
6+
* 2. String Comparison
7+
* 3. String Search
8+
* 4. String Decomposition
9+
* 5. String Replace
10+
*/
11+
12+
// 1. String Access
13+
'Hello world'.charAt(1); // returns 'e';
14+
'YouTube'.substring(1, 2); // returns 'o' : multiple-character access.
15+
'YouTube'.substring(3, 7); // returns 'Tube'
16+
'YouTube'.substring(1); // returns 'ouTube'
17+
18+
// 2. String Comparison
19+
const a = 'a';
20+
const b = 'b';
21+
22+
console.log(a < b); // Prints true;
23+
24+
const c = 'add';
25+
const d = 'ab';
26+
27+
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)