You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the following sections we'll see more examples of this.
204
+
Di bab ini kita akan melihat contoh yang lebih banyak dari ini.
205
205
206
-
## Changing the case
206
+
## Mengganti case dari string
207
207
208
-
Methods[toLowerCase()](mdn:js/String/toLowerCase)and[toUpperCase()](mdn:js/String/toUpperCase)change the case:
208
+
Method[toLowerCase()](mdn:js/String/toLowerCase)dan[toUpperCase()](mdn:js/String/toUpperCase)mengganti case dari string:
209
209
210
210
```js run
211
211
alert( 'Interface'.toUpperCase() ); // INTERFACE
212
212
alert( 'Interface'.toLowerCase() ); // interface
213
213
```
214
214
215
-
Or, if we want a single character lowercased:
215
+
Atau, apabila kita hanya ingin sebuah karakter yang diubah menjadi huruf kecil:
216
216
217
217
```js
218
218
alert( 'Interface'[0].toLowerCase() ); // 'i'
219
219
```
220
220
221
-
## Searching for a substring
221
+
## Mencari sebuah substring
222
222
223
-
There are multiple ways to look for a substring within a string.
223
+
Ada banyak cara untuk mencari sebuah substring di dalam sebuah string.
224
224
225
225
### str.indexOf
226
226
227
-
The first method is[str.indexOf(substr, pos)](mdn:js/String/indexOf).
227
+
Cara yang pertama yaitu[str.indexOf(substr, pos)](mdn:js/String/indexOf).
228
228
229
-
It looks for the `substr`in `str`, starting from the given position `pos`, and returns the position where the match was found or `-1`if nothing can be found.
229
+
Method ini mencari `substr`di dalam `str`, mulai dari posisi `pos` yang diberikan, dan mengembalikan posisi dimana substring ditemukan atau `-1`jika tidak ditemukan.
230
230
231
-
For instance:
231
+
Sebagai contoh:
232
232
233
233
```js run
234
234
let str ='Widget with id';
@@ -239,17 +239,17 @@ alert( str.indexOf('widget') ); // -1, not found, the search is case-sensitive
239
239
alert( str.indexOf("id") ); // 1, "id" is found at the position 1 (..idget with id)
240
240
```
241
241
242
-
The optional second parameter allows us to search starting from the given position.
242
+
Parameter kedua yang opsional memperbolehkan kita untuk mencari dari posisi yang ditentukan.
243
243
244
-
For instance, the first occurrence of `"id"`is at position `1`. To look for the next occurrence, let's start the search from position`2`:
244
+
Sebagai contoh, `"id"`muncul pertama pada posisi `1`. Untuk mencari dimana yang selanjutnya terletak, mari kita mulai mencari dari posisi`2`:
245
245
246
246
```js run
247
247
let str ='Widget with id';
248
248
249
249
alert( str.indexOf('id', 2) ) // 12
250
250
```
251
251
252
-
If we're interested in all occurrences, we can run`indexOf`in a loop. Every new call is made with the position after the previous match:
252
+
Jika kita tertarik dengan semua kemunculan, kita dapat menjalankan`indexOf`di dalam sebuah perulangan. Setiap panggilan dibuat dengan posisi dari kemunculan sebelumnya:
There is also a similar method[str.lastIndexOf(substr, position)](mdn:js/String/lastIndexOf)that searches from the end of a string to its beginning.
284
+
Ada juga method yang hampir sama[str.lastIndexOf(substr, position)](mdn:js/String/lastIndexOf)yang mencari dari akhir sebuah string sampai ke awalnya.
285
285
286
-
It would list the occurrences in the reverse order.
286
+
Cara tersebut akan menemukan kemunculan dalam urutan yang terbalik.
287
287
```
288
288
289
-
There is a slight inconvenience with `indexOf` in the `if` test. We can't put it in the `if` like this:
289
+
Ada sedikit kerepotan dalam menggunakan `indexOf` di dalam `if`. Kita tidak dapat menggunakannya seperti ini:
290
290
291
291
```js run
292
292
let str = "Widget with id";
@@ -296,9 +296,9 @@ if (str.indexOf("Widget")) {
296
296
}
297
297
```
298
298
299
-
The `alert` in the example above doesn't show because `str.indexOf("Widget")`returns`0` (meaning that it found the match at the starting position). Right, but `if`considers`0`to be`false`.
299
+
Contoh di atas tidak bekerja karena `str.indexOf("Widget")`mengembalikan`0` (artinya kemunculan ditemukan di awal string). `if`menganggap`0`sebagai`false`.
300
300
301
-
So, we should actually check for`-1`, like this:
301
+
Jadi, kita harus mengecek dengan nilai`-1`, seperti ini:
0 commit comments