File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -25,3 +25,30 @@ const c = 'add';
25
25
const d = 'ab' ;
26
26
27
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