1
+ // String Methods
2
+
3
+
4
+ //String.fromCharCode()
5
+ console . log ( String . fromCharCode ( 246 , 199 ) ) ; //return "too"
6
+
7
+ _____________________________________________________________________________________________________________________________________________________________________________________________________
8
+ //String.raw()
9
+ //Escape expressions such as **`\n`**, **`\t`**, **`\\`** (leaving a space, going to the next line, etc.) ensure that the string value remains as a string value without performing its function. .
10
+ console . log ( `hello\njavascript` ) ;
11
+ /*"hello
12
+ javascript"*/
13
+ console . log ( String . raw `hello\njavascript` ) ;
14
+ // "hello javascript"
15
+
16
+ _____________________________________________________________________________________________________________________________________________________________________________________________________
17
+ //length()
18
+ const myString = "bluebells" ;
19
+ console . log ( myString . length ) ; //return 9
20
+ myString . length = 4 ; //Provides no effect. The value and length are the same.
21
+
22
+ ______________________________________________________________________________________________________________________________________________________________________________________________________
23
+ //concat()
24
+ const str11 = "Hello" ;
25
+ const str21 = "World" ;
26
+ console . log ( str11 . concat ( " " , str21 ) ) ; //return "Hello World"
27
+ const arr = [ "Merhaba" , "!" , "Sisteme" , " " , "Hoşgeldiniz" ] ;
28
+ console . log ( "Burak" . concat ( ...arr ) ) ; //return "Burak Merhaba!Sisteme Hoşgeldiniz."
29
+ const arr2 = [ 1 , 2 , 3 , 4 ] ;
30
+ console . log ( "" . concat ( ...arr2 ) ) ; //return "1234";
31
+
32
+ _______________________________________________________________________________________________________________________________________________________________________________________________________
33
+ //endsWith()
34
+ const str12 = "Cats are the best!" ;
35
+ console . log ( str12 . endsWith ( "best!" ) ) ; //return true
36
+ const str2 = "Hello World" ;
37
+ console . log ( str2 . endsWith ( "llo" , 5 ) ) ; //return true
38
+ //(value,endPosition)
39
+ // Search for the value "llo" starting from the value at index 5 and going backwards (index 4. 3).```
40
+
41
+ _______________________________________________________________________________________________________________________________________________________________________________________________________
42
+ //includes()
43
+ const sentence = "The quick brown fox jumps over the lazy dog." ;
44
+ const searchChars = "FoX" ;
45
+ console . log (
46
+ `Bu cümlede ${ searchChars } kelimesi ${
47
+ sentence . toLowerCase ( ) . includes ( searchChars . toLowerCase ( ) ) ? "geçiyor." : "geçmiyor."
48
+ } `
49
+ ) ;
50
+ // return "Bu cümlede FoX kelimesi geçiyor"
51
+
52
+ _______________________________________________________________________________________________________________________________________________________________________________________________________
53
+ //indexOf()
54
+ const paragraph = "The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?" ;
55
+ console . log ( paragraph . indexOf ( "quick" ) ) ; //return 4 (4. indexten sonra bu kelime var)
56
+ console . log ( paragraph . indexOf ( "test" ) ) ; //return -1 (yok)
57
+
58
+ _______________________________________________________________________________________________________________________________________________________________________________________________________
59
+ //localeCompare()
60
+ //It compares two strings according to a given locale and arrangement and returns the results. When performing string comparison, text order, case, diacritics, and other language features are taken into account. Therefore, it is preferred to get correct string ordering according to language and localization rules. This way string sequences containing characters from different languages and special markings and diacritics can be handled properly.
61
+ `localeCompare(compareString, locales, options)` ;
62
+ // 1. If **`string`** is less than **`compareString`**, `-1` is returned.
63
+ // 2. If **`string`** and **`compareString`** are equal, **`0`** is returned.
64
+ // 3. If **`string`** is greater than **`compareString`**, **`1`** is returned.
65
+ console . log ( "apple" . localeCompare ( "banana" ) ) ; //return -1 (dize1 , dize2 den küçük)
66
+ console . log ( "apple" . localeCompare ( "apple" ) ) ; //return 0 (eşit)
67
+ console . log ( "Café" . localeCompare ( "Café" , undefined , { sensitivity : "variant" } ) ) ; //return -1
68
+ console . log ( "cafe" . localeCompare ( "Café" , undefined , { sensitivity : "base" } ) ) ; //return 1
69
+ console . log ( "Cafe" . localeCompare ( "Café" , undefined , { sensitivity : "case" } ) ) ; //return 0
70
+ console . log ( "cafe" . localeCompare ( "Café" , undefined , { sensitivity : "accent" } ) ) ; //return 0
71
+
72
+ _______________________________________________________________________________________________________________________________________________________________________________________________________
73
+ //padStart()
74
+ //It iterates to the beginning of a string value up to a certain length, filling it with the corresponding value.
75
+ let str = "45" ;
76
+ console . log ( str . padStart ( 5 , "1" ) ) ; // "11145"
77
+ const cardNumber = "2034399002125581" ;
78
+ const private = cardNumber . slice ( - 4 ) . padStart ( cardNumber . length , "*" ) ;
79
+ console . log ( private ) ; // "************5581"
80
+
81
+ _______________________________________________________________________________________________________________________________________________________________________________________________________
82
+ //padEnd()
83
+ //Repeats a string to the end of the value and fills it with the corresponding value.
84
+ let strn = "Hello World" ;
85
+ console . log ( strn . padEnd ( 14 , "." ) ) ; // "Hello World..."
86
+ console . log ( strn . padEnd ( strn . length + 3 , "!" ) ) ; // "Hello World!!!"
87
+ console . log ( "abc" . padEnd ( 10 ) ) ; // "abc "
88
+ console . log ( "abc" . padEnd ( 6 , "123456" ) ) ; // "abc123"
89
+
90
+ _______________________________________________________________________________________________________________________________________________________________________________________________________
91
+ //repead()
92
+ //Returns a new string that repeats the relevant string value the specified number of times.
93
+ const cardNumber1 = "123" ;
94
+ console . log ( cardNumber1 . repeat ( 2 ) ) ; // "123123"
95
+
96
+ _______________________________________________________________________________________________________________________________________________________________________________________________________
97
+ //replace()
98
+ const strnn = "Ali ata bak" ;
99
+ const strnnn = "Ali ata bak ata" ;
100
+ console . log ( strnn . replace ( "ata" , "arabaya" ) ) ; // "Ali arabaya bak"
101
+ console . log ( strnnn . replace ( "ata" , "arabaya" ) ) ; // "Ali arabaya bak ata"
102
+
103
+ _______________________________________________________________________________________________________________________________________________________________________________________________________
104
+ //replaceAll()
105
+ const strng = "Ali ata bak ata" ;
106
+ console . log ( strng . replaceAll ( "ata" , "arabaya" ) ) ; // "Ali arabaya bak arabaya"
107
+
108
+ _______________________________________________________________________________________________________________________________________________________________________________________________________
109
+ //slice()
110
+ const str111 = "The quick brown fox jumps over the lazy dog." ;
111
+ console . log ( str111 . slice ( 31 ) ) ; // Expected output: "the lazy dog."
112
+ console . log ( str111 . slice ( 4 , 19 ) ) ; // Expected output: "quick brown fox"
113
+ console . log ( str111 . slice ( - 4 ) ) ; // Expected output: "dog."
114
+ console . log ( str111 . slice ( - 9 , - 5 ) ) ; // Expected output: "lazy"
115
+
116
+ _______________________________________________________________________________________________________________________________________________________________________________________________________
117
+ //split()
118
+ //Searches the value specified in the related string expression and splits it into parts from this value and returns it as an array.
119
+ const str222 = "Ali ata bak atın rengi kırmızı" ;
120
+ console . log ( str222 . split ( "ata" ) ) ; //["Ali "," bak atın rengi kırmızı"]
121
+ console . log ( str222 . split ( "ata" ) ) ; //["Ali","ata","bak","atın","rengi","kırmızı"]
122
+ console . log ( str222 . split ( " " , 3 ) ) ; //["Ali","ata","bak"] (en fazla 3 değer döndür)
123
+
124
+ _______________________________________________________________________________________________________________________________________________________________________________________________________
125
+ //startWith()
126
+ const str1 = "Saturday night plans" ;
127
+ console . log ( str1 . startsWith ( "Sat" ) ) ; // true
128
+ console . log ( str1 . startsWith ( "Sat" , 3 ) ) ; // false (3. indexten itibaren kontrol et)
129
+ console . log ( str1 . startsWith ( "urd" , 3 ) ) ; // true
130
+
131
+ _______________________________________________________________________________________________________________________________________________________________________________________________________
132
+ //substring()
133
+ //It returns the value according to the starting and ending index of the relevant string.
134
+ const str3 = "Mozilla" ;
135
+ console . log ( str3 . substring ( 1 , 3 ) ) ; // "oz"
136
+ console . log ( str3 . substring ( 2 ) ) ; // "zilla" (bitiş index değeri yoksa sonuna kadar keser)
137
+ console . log ( str3 . substring ( 2 , 1 ) ) ; // "oz" (sondan başa doğru da aynı değer alınabilir)
138
+
139
+ _______________________________________________________________________________________________________________________________________________________________________________________________________
140
+ //toLocaleLowerCase() , toLocaleUpperCase()
141
+ console . log ( "DUBAI GÜZEL" . toLocaleLowerCase ( "tr" ) ) ; // dubaı güzel
142
+ console . log ( "DUBAI GÜZEL" . toLocaleLowerCase ( "en-US" ) ) ; // dubai güzel
143
+
144
+ _______________________________________________________________________________________________________________________________________________________________________________________________________
145
+ //toString()
146
+ const nbr = 42 ;
147
+ console . log ( nbr . toString ( ) ) ; // "42"
148
+ const array = [ 1 , 2 , 3 ] ;
149
+ console . log ( array . toString ( ) ) ; // "1,2,3"
150
+ const boolValue = true ;
151
+ const strBool = boolValue . toString ( ) ; // "true"
152
+ const obj = { name : "John" , age : 30 } ;
153
+ console . log ( obj . toString ( ) ) ; // çevrilmez
154
+
155
+ _______________________________________________________________________________________________________________________________________________________________________________________________________
156
+ //trim()
157
+ const str4 = " ali mali " ;
158
+ console . log ( str4 . trim ( ) ) ; // "ali mali"
159
+
160
+ _______________________________________________________________________________________________________________________________________________________________________________________________________
161
+ //trimStart()
162
+ const str5 = " ali mali " ;
163
+ console . log ( str5 . trim ( ) ) ; // "ali mali "
164
+
165
+ _______________________________________________________________________________________________________________________________________________________________________________________________________
166
+ //trimEnd()
167
+ const str6 = " ali mali " ;
168
+ console . log ( str6 . trim ( ) ) ; // " ali mali"
169
+
170
+ _______________________________________________________________________________________________________________________________________________________________________________________________________
171
+ //charAt()
172
+ const str7 = "Ali ata bak" ;
173
+ console . log ( str7 . charAt ( 2 ) ) ; //return "i"
174
+ _______________________________________________________________________________________________________________________________________________________________________________________________________
0 commit comments