File tree Expand file tree Collapse file tree 1 file changed +54
-2
lines changed Expand file tree Collapse file tree 1 file changed +54
-2
lines changed Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments