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
Copy file name to clipboardExpand all lines: 1-js/05-data-types/03-string/article.md
+43-43Lines changed: 43 additions & 43 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -216,55 +216,55 @@ Lub, jeśli chcemy, aby jeden znak był pisany małymi literami:
216
216
alert( 'Interface'[0].toLowerCase() ); // 'i'
217
217
```
218
218
219
-
## Searching for a substring
219
+
## Wyszukiwanie podciągu
220
220
221
-
There are multiple ways to look for a substring within a string.
221
+
Istnieje wiele sposobów wyszukiwania podciągu w ciągu.
222
222
223
223
### str.indexOf
224
224
225
-
The first method is[str.indexOf(substr, pos)](mdn:js/String/indexOf).
225
+
Pierwszą metodą jest[str.indexOf(substr, pos)](mdn:js/String/indexOf).
226
226
227
-
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.
227
+
Szuka `substr`w`str`, zaczynając od podanej pozycji `pos`, i zwraca pozycję, w której znalazł dopasowanie lub `-1`jeśli nic nie zostało znalezione.
228
228
229
-
For instance:
229
+
Na przykład:
230
230
231
231
```js run
232
232
let str ='Widget with id';
233
233
234
-
alert( str.indexOf('Widget') ); // 0, because 'Widget' is found at the beginning
235
-
alert( str.indexOf('widget') ); // -1, not found, the search is case-sensitive
234
+
alert( str.indexOf('Widget') ); // 0, because 'Widget' został znaleziony na początku łańcucha
235
+
alert( str.indexOf('widget') ); // -1, nie znaleziono, w wyszukiwaniu rozróżniana jest wielkość liter
236
236
237
-
alert( str.indexOf("id") ); // 1, "id" is found at the position 1 (..idget with id)
237
+
alert( str.indexOf("id") ); // 1, "id" znajduje się na pozycji 1 (id w Widget)
238
238
```
239
239
240
-
The optional second parameter allows us to search starting from the given position.
240
+
Opcjonalny drugi parametr pozwala nam na wyszukiwanie zaczynając od podanej pozycji.
241
241
242
-
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`:
242
+
Na przykład pierwsze wystąpienie `"id"`występuje na pozycji`1`. Aby wyszukać następne wystąpienie, zacznijmy wyszukiwanie od pozycji`2`:
243
243
244
244
```js run
245
245
let str ='Widget with id';
246
246
247
247
alert( str.indexOf('id', 2) ) // 12
248
248
```
249
249
250
-
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:
250
+
Jeśli interesują nas wszystkie wystąpienia, możemy uruchomić `indexOf`w pętli. Każde nowe wywołanie jest wykonywane na następnej pozycji w łańcuchu po poprzednim dopasowaniu:
251
251
252
252
```js run
253
253
let str ='As sly as a fox, as strong as an ox';
254
254
255
-
let target ='as'; //let's look for it
255
+
let target ='as'; //cel wyszukiwania
256
256
257
257
let pos =0;
258
258
while (true) {
259
259
let foundPos =str.indexOf(target, pos);
260
260
if (foundPos ==-1) break;
261
261
262
262
alert( `Found at ${foundPos}` );
263
-
pos = foundPos +1; //continue the search from the next position
263
+
pos = foundPos +1; //kontynuuj wyszukiwanie od następnej pozycji
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.
282
+
Istnieje również podobna metoda [str.lastIndexOf(substr, position)](mdn:js/String/lastIndexOf), która przeszukuje string od końca do jego początku.
283
283
284
-
It would list the occurrences in the reverse order.
284
+
Zwraca wystąpienia w odwrotnej kolejności.
285
285
```
286
286
287
-
There is a slight inconvenience with `indexOf` in the `if` test. We can't put it in the `if` like this:
287
+
Istnieje niewielka niedogodność z metodą `indexOf` w sprawdzeniu warunkowym `if`. Ten warunek nie zadziała prawidłowo:
288
288
289
289
```js run
290
290
let str = "Widget with id";
291
291
292
292
if (str.indexOf("Widget")) {
293
-
alert("We found it"); // doesn't work!
293
+
alert("We found it"); // Nie zadziała!
294
294
}
295
295
```
296
296
297
-
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`.
297
+
`alert`w powyższym przykładzie nie jest wyświetlany, ponieważ`str.indexOf("Widget")`zwraca`0` (co oznacza, że znalazł dopasowanie na pozycji wyjściowej). Natomiast warunek`if`odczytuje`0`jako`false`.
298
298
299
-
So, we should actually check for `-1`, like this:
299
+
Dlatego też powinniśmy użyć warunku dla wartości `-1`:
300
300
301
301
```js run
302
302
let str ="Widget with id";
@@ -308,69 +308,69 @@ if (str.indexOf("Widget") != -1) {
308
308
}
309
309
```
310
310
311
-
#### The bitwise NOT trick
311
+
#### Bitowy trik NOT
312
312
313
-
One of the old tricks used here is the [bitwise NOT](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators#Bitwise_NOT)`~` operator. It converts the number to a 32-bit integer (removes the decimal part if exists) and then reverses all bits in its binary representation.
313
+
Istnieje stara sztuczka z użyciem [bitowego operatora NOT](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_NOT)`~`. Konwertuje liczbę na 32-bitową liczbę całkowitą (usuwa część dziesiętną, jeśli istnieje), a następnie odwraca wszystkie bity w reprezentacji binarnej.
314
314
315
-
In practice, that means a simple thing: for 32-bit integers `~n`equals`-(n+1)`.
315
+
W praktyce oznacza to, że: dla 32-bitowych liczb całkowitych `~n`równa się`-(n+1)`.
316
316
317
-
For instance:
317
+
Na przykład:
318
318
319
319
```js run
320
-
alert( ~2 ); // -3, the same as -(2+1)
321
-
alert( ~1 ); // -2, the same as -(1+1)
322
-
alert( ~0 ); // -1, the same as -(0+1)
320
+
alert( ~2 ); // -3, to samo, co -(2+1)
321
+
alert( ~1 ); // -2, to samo, co -(1+1)
322
+
alert( ~0 ); // -1, to samo, co -(0+1)
323
323
*!*
324
-
alert( ~-1 ); // 0, the same as -(-1+1)
324
+
alert( ~-1 ); // 0, to samo, co -(-1+1)
325
325
*/!*
326
326
```
327
327
328
-
As we can see, `~n`is zero only if `n == -1` (that's for any 32-bit signed integer`n`).
328
+
Zatem `~n`jest równe zeru, tylko, gdy `n == -1` (dowolna 32-bitowa liczby całkowitej ze znakiem`n`).
329
329
330
-
So, the test`if ( ~str.indexOf("...") )`is truthy only if the result of `indexOf`is not`-1`. In other words, when there is a match.
330
+
Tak więc warunek`if ( ~str.indexOf("...") )`jest prawdziwy tylko wtedy, gdy wynikiem `indexOf`nie jest`-1`. Innymi słowy, kiedy jest dopasowanie.
331
331
332
-
People use it to shorten`indexOf` checks:
332
+
Ludzie używają go do skrócenia`indexOf`:
333
333
334
334
```js run
335
335
let str ="Widget";
336
336
337
337
if (~str.indexOf("Widget")) {
338
-
alert( 'Found it!' ); //works
338
+
alert( 'Found it!' ); //działa
339
339
}
340
340
```
341
341
342
-
It is usually not recommended to use language features in a non-obvious way, but this particular trick is widely used in old code, so we should understand it.
342
+
Generalnie odradza się używanie funkcji językowych w jakikolwiek nieoczywisty sposób, ale ta sztuczka jest szeroko stosowana w starszym kodzie, więc ważne jest, aby ją zrozumieć.
343
343
344
-
Just remember: `if (~str.indexOf(...))`reads as "if found".
344
+
Wystarczy, że zapamiętasz: `if (~str.indexOf(...))`oznacza "jeśli znaleziono".
345
345
346
-
To be precise though, as big numbers are truncated to 32 bits by `~`operator, there exist other numbers that give `0`, the smallest is `~4294967295=0`. That makes such check is correct only if a string is not that long.
346
+
Aby być precyzyjnym, należy wspomnieć, że z powodu iż, duże liczby są obcinane przez operator `~`do 32 bitów, istnieją inne liczby, które dają 0. Najmniejsza to `~4294967295=0`. To sprawia, że takie sprawdzenie jest poprawne tylko wtedy, gdy łańcuch nie jest tak długi.
347
347
348
-
Right now we can see this trick only in the old code, as modern JavaScript provides `.includes` method (see below).
348
+
Aktualnie tę sztuczkę możemy zobaczyć tylko w starym kodzie, ponieważ współczesny JavaScript zapewnia metodę .includes (patrz poniżej).
349
349
350
350
### includes, startsWith, endsWith
351
351
352
-
The more modern method [str.includes(substr, pos)](mdn:js/String/includes)returns`true/false`depending on whether`str`contains `substr` within.
352
+
Bardziej nowoczesna metoda [str.includes(substr, pos)](mdn:js/String/includes)zwraca`true/false`w zależności, czy`str`zawiera w sobie `substr`.
353
353
354
-
It's the right choice if we need to test for the match, but don't need its position:
354
+
To właściwy wybór, jeśli musimy sprawdzić wystąpienie jakiegoś podciągu, ale nie interesuje nas jego pozycja w łańcuchu:
355
355
356
356
```js run
357
357
alert( "Widget with id".includes("Widget") ); // true
358
358
359
359
alert( "Hello".includes("Bye") ); // false
360
360
```
361
361
362
-
The optional second argument of `str.includes`is the position to start searching from:
362
+
Opcjonalny drugi argument `str.includes`to pozycja, od której należy rozpocząć wyszukiwanie:
363
363
364
364
```js run
365
365
alert( "Widget".includes("id") ); // true
366
-
alert( "Widget".includes("id", 3) ); // false, from position 3 there is no "id"
366
+
alert( "Widget".includes("id", 3) ); // false, od pozycji 3 "id nie występuje
367
367
```
368
368
369
-
The methods [str.startsWith](mdn:js/String/startsWith)and[str.endsWith](mdn:js/String/endsWith)do exactly what they say:
369
+
Metody [str.startsWith](mdn:js/String/startsWith)i[str.endsWith](mdn:js/String/endsWith)sprawdzają odpowiednio, czy łańcuch zaczyna się i kończy na określonym podciągu:
370
370
371
371
```js run
372
-
alert( "Widget".startsWith("Wid") ); // true, "Widget" starts with "Wid"
373
-
alert( "Widget".endsWith("get") ); // true, "Widget" ends with "get"
372
+
alert( "Widget".startsWith("Wid") ); // true, "Widget" zaczyna się od "Wid"
373
+
alert( "Widget".endsWith("get") ); // true, "Widget" kończy się na "get"
0 commit comments