Skip to content

Commit e2c28bf

Browse files
string#searching-for-a-substring
1 parent 5100f5f commit e2c28bf

File tree

1 file changed

+43
-43
lines changed

1 file changed

+43
-43
lines changed

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

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -216,55 +216,55 @@ Lub, jeśli chcemy, aby jeden znak był pisany małymi literami:
216216
alert( 'Interface'[0].toLowerCase() ); // 'i'
217217
```
218218

219-
## Searching for a substring
219+
## Wyszukiwanie podciągu
220220

221-
There are multiple ways to look for a substring within a string.
221+
Istnieje wiele sposobów wyszukiwania podciągu w ciągu.
222222

223223
### str.indexOf
224224

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).
226226

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.
228228

229-
For instance:
229+
Na przykład:
230230

231231
```js run
232232
let str = 'Widget with id';
233233

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
236236

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)
238238
```
239239

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.
241241

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`:
243243

244244
```js run
245245
let str = 'Widget with id';
246246

247247
alert( str.indexOf('id', 2) ) // 12
248248
```
249249

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:
251251

252252
```js run
253253
let str = 'As sly as a fox, as strong as an ox';
254254

255-
let target = 'as'; // let's look for it
255+
let target = 'as'; // cel wyszukiwania
256256

257257
let pos = 0;
258258
while (true) {
259259
let foundPos = str.indexOf(target, pos);
260260
if (foundPos == -1) break;
261261

262262
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
264264
}
265265
```
266266

267-
The same algorithm can be layed out shorter:
267+
Ten sam algorytm można skrócić:
268268

269269
```js run
270270
let str = "As sly as a fox, as strong as an ox";
@@ -279,24 +279,24 @@ while ((pos = str.indexOf(target, pos + 1)) != -1) {
279279
```
280280

281281
```smart header="`str.lastIndexOf(substr, position)`"
282-
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.
283283

284-
It would list the occurrences in the reverse order.
284+
Zwraca wystąpienia w odwrotnej kolejności.
285285
```
286286
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:
288288
289289
```js run
290290
let str = "Widget with id";
291291
292292
if (str.indexOf("Widget")) {
293-
alert("We found it"); // doesn't work!
293+
alert("We found it"); // Nie zadziała!
294294
}
295295
```
296296

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`.
298298

299-
So, we should actually check for `-1`, like this:
299+
Dlatego też powinniśmy użyć warunku dla wartości `-1`:
300300

301301
```js run
302302
let str = "Widget with id";
@@ -308,69 +308,69 @@ if (str.indexOf("Widget") != -1) {
308308
}
309309
```
310310

311-
#### The bitwise NOT trick
311+
#### Bitowy trik NOT
312312

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.
314314

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)`.
316316

317-
For instance:
317+
Na przykład:
318318

319319
```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)
323323
*!*
324-
alert( ~-1 ); // 0, the same as -(-1+1)
324+
alert( ~-1 ); // 0, to samo, co -(-1+1)
325325
*/!*
326326
```
327327

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`).
329329

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.
331331

332-
People use it to shorten `indexOf` checks:
332+
Ludzie używają go do skrócenia `indexOf`:
333333

334334
```js run
335335
let str = "Widget";
336336

337337
if (~str.indexOf("Widget")) {
338-
alert( 'Found it!' ); // works
338+
alert( 'Found it!' ); // działa
339339
}
340340
```
341341

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ć.
343343

344-
Just remember: `if (~str.indexOf(...))` reads as "if found".
344+
Wystarczy, że zapamiętasz: `if (~str.indexOf(...))` oznacza "jeśli znaleziono".
345345

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.
347347

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).
349349

350350
### includes, startsWith, endsWith
351351

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`.
353353

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:
355355

356356
```js run
357357
alert( "Widget with id".includes("Widget") ); // true
358358

359359
alert( "Hello".includes("Bye") ); // false
360360
```
361361

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:
363363

364364
```js run
365365
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
367367
```
368368

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:
370370

371371
```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"
374374
```
375375

376376
## Getting a substring

0 commit comments

Comments
 (0)