Skip to content

Commit 075ea56

Browse files
string#getting-a-substring
1 parent e2c28bf commit 075ea56

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

1-js/05-data-types/03-string/article.md

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -373,88 +373,88 @@ alert( "Widget".startsWith("Wid") ); // true, "Widget" zaczyna się od "Wid"
373373
alert( "Widget".endsWith("get") ); // true, "Widget" kończy się na "get"
374374
```
375375

376-
## Getting a substring
376+
## Pobieranie podciągu
377377

378-
There are 3 methods in JavaScript to get a substring: `substring`, `substr` and `slice`.
378+
JavaScript ma 3 metody uzyskiwania podciągu: `substring`, `substr` i `slice`.
379379

380380
`str.slice(start [, end])`
381-
: Returns the part of the string from `start` to (but not including) `end`.
381+
: Zwraca część łańcucha od `start` do `end` (ale go nie uwzględnia).
382382

383-
For instance:
383+
Na przykład:
384384

385385
```js run
386386
let str = "stringify";
387-
alert( str.slice(0, 5) ); // 'strin', the substring from 0 to 5 (not including 5)
388-
alert( str.slice(0, 1) ); // 's', from 0 to 1, but not including 1, so only character at 0
387+
alert( str.slice(0, 5) ); // 'strin', podciąg od 0 do 5 (nie uwzględnia 5)
388+
alert( str.slice(0, 1) ); // 's', od 0 do 1, ale nie uwzględnia 1, czyli tylko jeden znak z pozycji 0
389389
```
390390

391-
If there is no second argument, then `slice` goes till the end of the string:
391+
Jeśli nie ma drugiego argumentu, `slice` zwraca znaki do końca linii:
392392

393393
```js run
394394
let str = "st*!*ringify*/!*";
395-
alert( str.slice(2) ); // 'ringify', from the 2nd position till the end
395+
alert( str.slice(2) ); // 'ringify', od drugiej pozycji do końca
396396
```
397397

398-
Negative values for `start/end` are also possible. They mean the position is counted from the string end:
398+
Możliwe są również ujemne wartości `start/end`. Oznacza to, że pozycja jest liczona od końca ciągu:
399399

400400
```js run
401401
let str = "strin*!*gif*/!*y";
402402

403-
// start at the 4th position from the right, end at the 1st from the right
403+
// zaczynamy od pozycji 4 od prawej i kończymy na pozycji 1 od prawej
404404
alert( str.slice(-4, -1) ); // 'gif'
405405
```
406406

407407
`str.substring(start [, end])`
408-
: Returns the part of the string *between* `start` and `end`.
408+
: Zwraca część ciągu _pomiędzy_ `start` i `end`.
409409

410-
This is almost the same as `slice`, but it allows `start` to be greater than `end`.
410+
Jest to prawie to samo, co `slice`, z tą różnicą, że `start` może być większe niż `end`.
411411

412-
For instance:
412+
Na przykład:
413413

414414
```js run
415415
let str = "st*!*ring*/!*ify";
416416

417-
// these are same for substring
417+
// dla substring te dwa przykłady są takie same
418418
alert( str.substring(2, 6) ); // "ring"
419419
alert( str.substring(6, 2) ); // "ring"
420420

421-
// ...but not for slice:
422-
alert( str.slice(2, 6) ); // "ring" (the same)
423-
alert( str.slice(6, 2) ); // "" (an empty string)
421+
// ...ale nie dla slice:
422+
alert( str.slice(2, 6) ); // "ring" (to samo)
423+
alert( str.slice(6, 2) ); // "" (pusty łańcuch)
424424

425425
```
426426

427-
Negative arguments are (unlike slice) not supported, they are treated as `0`.
427+
`substring` w przeciwieństwie do `slice` nie obsługuje wartości ujemnych i interpretuje je jako `0`.
428428

429429
`str.substr(start [, length])`
430-
: Returns the part of the string from `start`, with the given `length`.
430+
: Zwraca część ciągu od `start` do podanej długości `length`.
431431

432-
In contrast with the previous methods, this one allows us to specify the `length` instead of the ending position:
432+
W przeciwieństwie do poprzednich metod, ta umożliwia określenie długości `length` zamiast pozycji końcowej:
433433

434434
```js run
435435
let str = "st*!*ring*/!*ify";
436-
alert( str.substr(2, 4) ); // 'ring', from the 2nd position get 4 characters
436+
alert( str.substr(2, 4) ); // 'ring', 4 znaki liczone od drugiej pozycji
437437
```
438438

439-
The first argument may be negative, to count from the end:
439+
Wartość pierwszego argumentu może być ujemna, w takim przypadku pozycja określana jest od końca:
440440

441441
```js run
442442
let str = "strin*!*gi*/!*fy";
443-
alert( str.substr(-4, 2) ); // 'gi', from the 4th position get 2 characters
443+
alert( str.substr(-4, 2) ); // 'gi', 2 znaki liczone od czwartej pozycji od końca
444444
```
445445

446-
Let's recap these methods to avoid any confusion:
446+
Podsumujmy te metody, aby uniknąć nieporozumień:
447447

448-
| method | selects... | negatives |
448+
| metoda | wybiera... | wartości ujemne |
449449
|--------|-----------|-----------|
450-
| `slice(start, end)` | from `start` to `end` (not including `end`) | allows negatives |
451-
| `substring(start, end)` | between `start` and `end` | negative values mean `0` |
452-
| `substr(start, length)` | from `start` get `length` characters | allows negative `start` |
450+
| `slice(start, end)` | od `start` do `end` (bez uwzględnienia `end`) | zezwala |
451+
| `substring(start, end)` | pomiędzy `start` i `end` | wartości ujemne oznaczają `0` |
452+
| `substr(start, length)` | `length` znaków od `start` | pozwala na wartość ujemną dla `start` |
453453

454-
```smart header="Which one to choose?"
455-
All of them can do the job. Formally, `substr` has a minor drawback: it is described not in the core JavaScript specification, but in Annex B, which covers browser-only features that exist mainly for historical reasons. So, non-browser environments may fail to support it. But in practice it works everywhere.
454+
```smart header="Którą metodę wybrać?"
455+
Wszystkie metody robią robotę. Formalnie `substr` ma niewielką wadę: nie jest opisana w podstawowej specyfikacji JavaScript, ale w załączniku B. Dodatek ten opisuje cechy języka używanego w przeglądarkach, które istnieją głównie ze względów historycznych. Dlatego środowiska inne niż przeglądarki mogą go nie obsługiwać. Jednak w praktyce działa wszędzie.
456456
457-
Of the other two variants, `slice` is a little bit more flexible, it allows negative arguments and shorter to write. So, it's enough to remember solely `slice` of these three methods.
457+
Z pozostałych dwóch opcji, `slice` jest nieco bardziej elastyczne - pozwala na użycie wartości ujemny i jest krótsze. Wystarczy więc, że spośród tych metoda zapamiętasz `slice`.
458458
```
459459

460460
## Comparing strings

0 commit comments

Comments
 (0)