func`string`
. The function `func` is called automatically, receives the string and embedded expressions and can process them. This is called "tagged templates". This feature makes it easier to implement custom templating, but is rarely used in practice. You can read more about it in the [manual](mdn:/JavaScript/Reference/Template_literals#Tagged_templates).
+Зворотні лапки також дозволяють нам задати "шаблонну функцію" перед першими зворотніми лапками. Синтаксис такий: func`string`
. Функція `func` викликається автоматично, отримує рядок і вбудовані в неї вирази і може їх обробити. Це називається "теговим шаблоном". Ця функція полегшує реалізацію користувацької шаблонізації, але рідко використовується на практиці. Детальніше про це можна прочитати в [посібнику](mdn:/JavaScript/Reference/Template_literals#Tagged_templates).
-## Special characters
+## Спеціальні символи
-It is still possible to create multiline strings with single and double quotes by using a so-called "newline character", written as `\n`, which denotes a line break:
+Ще можна створити багатолінійні рядки з одинарними та подвійними лапками за допомогою так званого "символу нового рядка", записаного як `\n`, який позначає розрив рядка:
```js run
-let guestList = "Guests:\n * John\n * Pete\n * Mary";
+let guestList = "Гості:\n * Іван\n * Петро\n * Марія";
-alert(guestList); // a multiline list of guests
+alert(guestList); // список гостей в декілька рядків
```
-For example, these two lines are equal, just written differently:
+Наприклад, ці два рядки рівнозначні, просто написані по-різному:
```js run
-let str1 = "Hello\nWorld"; // two lines using a "newline symbol"
+let str1 = "Привіт\nСвіт"; // два рядки з використанням "символу нового рядка"
-// two lines using a normal newline and backticks
-let str2 = `Hello
-World`;
+// два рядки з використанням звичайного нового рядка та зворотних лапок
+let str2 = `Привіт
+Світ`;
alert(str1 == str2); // true
```
-There are other, less common "special" characters.
+Є й інші, менш поширені "спеціальні" символи.
-Here's the full list:
+Ось повний список:
-| Character | Description |
+| Символ | Опис |
|-----------|-------------|
-|`\n`|New line|
-|`\r`|In Windows text files a combination of two characters `\r\n` represents a new break, while on non-Windows OS it's just `\n`. That's for historical reasons, most Windows software also understands `\n`. |
-|`\'`, `\"`|Quotes|
-|`\\`|Backslash|
-|`\t`|Tab|
-|`\b`, `\f`, `\v`| Backspace, Form Feed, Vertical Tab -- kept for compatibility, not used nowadays. |
-|`\xXX`|Unicode character with the given hexadecimal Unicode `XX`, e.g. `'\x7A'` is the same as `'z'`.|
-|`\uXXXX`|A Unicode symbol with the hex code `XXXX` in UTF-16 encoding, for instance `\u00A9` -- is a Unicode for the copyright symbol `©`. It must be exactly 4 hex digits. |
-|`\u{X…XXXXXX}` (1 to 6 hex characters)|A Unicode symbol with the given UTF-32 encoding. Some rare characters are encoded with two Unicode symbols, taking 4 bytes. This way we can insert long codes. |
+|`\n`|Розрив рядка|
+|`\r`|У текстових файлах Windows комбінація двох символів `\r\n` являє собою розрив рядка, тоді як в інших ОС, це просто `\n`. Так склалось з історичних причин, більшість ПЗ під Windows також розуміє `\n`|
+|`\'`, `\"`|Лапки|
+|`\\`|Зворотний слеш|
+|`\t`|Знак табуляції|
+|`\b`, `\f`, `\v`| Backspace, Form Feed, Vertical Tab – зберігаються для зворотної сумісності, зараз не використовуються|
+|`\xXX`|Символ з шістнадцятковим юнікодним кодом `'\x7A'` -- це те ж саме що і `'z'`|
+|`\uXXXX`|Символ з шістнадцятковим юнікодним кодом `XXXX` в кодуванні UTF-16, наприклад `\u00A9` -- це юнікодний символ для знаку копірайту `©`. Шістнадцятковий код обовʼязково має складатись з 4 символів|
+|`\u{X…XXXXXX}` (від 1 до 6 шістнадцяткових символів)|Юнікодний символ в кодуванні UTF-32. Деякі рідкісні символи кодуються двома юнікодними символами, що займають 4 байти. Таким чином ми можемо вставляти довгі коди|
-Examples with Unicode:
+Приклади з Юнікодом:
```js run
alert( "\u00A9" ); // ©
-alert( "\u{20331}" ); // 佫, a rare Chinese hieroglyph (long Unicode)
-alert( "\u{1F60D}" ); // 😍, a smiling face symbol (another long Unicode)
+alert( "\u{20331}" ); // 佫, рідкісний китайський ієрогліф (довгий юнікод)
+alert( "\u{1F60D}" ); // 😍, емодзі посмішки з очима в формі сердець (інший довгий юнікод)
```
-All special characters start with a backslash character `\`. It is also called an "escape character".
+Усі спеціальні символи починаються зі зворотного слеша `\`. Його також називають "символом екранування".
-We might also use it if we wanted to insert a quote into the string.
+Ми також можемо його використати, якщо хочемо вставити лапки в рядок.
-For instance:
+Наприклад:
```js run
-alert( 'I*!*\'*/!*m the Walrus!' ); // *!*I'm*/!* the Walrus!
+alert( 'Ім*!*\'*/!*я моє — Морж!' ); // *!*Ім'я*/!* моє — Морж!
```
-As you can see, we have to prepend the inner quote by the backslash `\'`, because otherwise it would indicate the string end.
+Як бачите, ми повинні "екранувати" лапку зворотним слешем `\'`, оскільки інакше це означало б кінець рядка.
-Of course, only the quotes that are the same as the enclosing ones need to be escaped. So, as a more elegant solution, we could switch to double quotes or backticks instead:
+Звісно, потрібно "екранувати" лише такі лапки, якими обрамлений рядок. Як елегантніше рішення, ми могли б замість цього скористатися подвійними або зворотними лапками:
```js run
-alert( `I'm the Walrus!` ); // I'm the Walrus!
+alert( `Ім'я моє — Морж!` ); // Ім'я моє — Морж!
```
-Note that the backslash `\` serves for the correct reading of the string by JavaScript, then disappears. The in-memory string has no `\`. You can clearly see that in `alert` from the examples above.
+Зверніть увагу, що зворотний слеш `\` в JavaScript служить для правильного зчитування рядка. Рядок в памʼяті не містить `\`. Ви можете чітко це побачити у `alert` з наведених вище прикладів.
-But what if we need to show an actual backslash `\` within the string?
+Але що, якщо нам потрібно показати зворотний слеш `\` всередині рядка?
-That's possible, but we need to double it like `\\`:
+Це можна зробити додаванням ще одного зворотного слеша `\\`:
```js run
-alert( `The backslash: \\` ); // The backslash: \
+alert( `Зворотний слеш: \\` ); // Зворотний слеш: \
```
-## String length
+## Довжина рядка
-The `length` property has the string length:
+Властивість `length` містить в собі довжину рядка:
```js run
-alert( `My\n`.length ); // 3
+alert( `Моє\n`.length ); // 4
```
-Note that `\n` is a single "special" character, so the length is indeed `3`.
+Зверніть увагу, що `\n` -- це один спеціальний символ, тому довжина рівна `4`.
-```warn header="`length` is a property"
-People with a background in some other languages sometimes mistype by calling `str.length()` instead of just `str.length`. That doesn't work.
+```warn header="`length` -- це властивість"
+Люди з досвідом роботи в інших мовах випадково намагаються викликати властивість, додаючи круглі дужки: вони пишуть `str.length()` замість `str.length`. Це не спрацює.
-Please note that `str.length` is a numeric property, not a function. There is no need to add parenthesis after it.
+Зверніть увагу, що `str.length` -- це числове значення, а не функція, додавати дужки не потрібно.
```
-## Accessing characters
+## Доступ до символів
-To get a character at position `pos`, use square brackets `[pos]` or call the method [str.charAt(pos)](mdn:js/String/charAt). The first character starts from the zero position:
+Отримати символ, котрий займає позицію `pos`, можна за допомогою квадратних дужок: `[pos]`, або викликати метод [str.charAt(pos)](mdn:js/String/charAt). Перший символ займає нульову позицію.
```js run
-let str = `Hello`;
+let str = `Привіт`;
-// the first character
-alert( str[0] ); // H
-alert( str.charAt(0) ); // H
+// перший символ
+alert( str[0] ); // П
+alert( str.charAt(0) ); // П
-// the last character
-alert( str[str.length - 1] ); // o
+// останній символ
+alert( str[str.length - 1] ); // т
```
-The square brackets are a modern way of getting a character, while `charAt` exists mostly for historical reasons.
+Квадратні дужки -- це сучасний спосіб отримати символ, тоді як `charAt` існує більше завдяки історичним причинам.
-The only difference between them is that if no character is found, `[]` returns `undefined`, and `charAt` returns an empty string:
+Різниця між ними лише в тому, що якщо символ з такою позицією відсутній, тоді `[]` поверне `undefined`, а `charAt` -- порожній рядок:
```js run
-let str = `Hello`;
+let str = `Привіт`;
alert( str[1000] ); // undefined
-alert( str.charAt(1000) ); // '' (an empty string)
+alert( str.charAt(1000) ); // '' (порожній рядок)
```
-We can also iterate over characters using `for..of`:
+Ми також можемо перебрати рядок посимвольно, використовуючи `for..of`:
```js run
-for (let char of "Hello") {
- alert(char); // H,e,l,l,o (char becomes "H", then "e", then "l" etc)
+for (let char of "Привіт") {
+ alert(char); // П,р,и,в,і,т (char — спочатку "П", потім "р", потім "и" і так далі)
}
```
-## Strings are immutable
+## Рядки незмінні
-Strings can't be changed in JavaScript. It is impossible to change a character.
+В JavaScript рядки не можна змінювати. Змінити символ неможливо.
-Let's try it to show that it doesn't work:
+Спробуємо показати на прикладі:
```js run
-let str = 'Hi';
+let str = 'Ой';
-str[0] = 'h'; // error
-alert( str[0] ); // doesn't work
+str[0] = 'о'; // помилка
+alert( str[0] ); // не працює
```
-The usual workaround is to create a whole new string and assign it to `str` instead of the old one.
+Можна створити новий рядок замість старого, записавши його в ту саму змінну.
-For instance:
+Ось так:
```js run
-let str = 'Hi';
+let str = 'Ой';
-str = 'h' + str[1]; // replace the string
+str = 'о' + str[1]; // замінюємо рядок
-alert( str ); // hi
+alert( str ); // ой
```
-In the following sections we'll see more examples of this.
+В наступних розділах ми побачимо більше прикладів.
-## Changing the case
+## Зміна регістру
-Methods [toLowerCase()](mdn:js/String/toLowerCase) and [toUpperCase()](mdn:js/String/toUpperCase) change the case:
+Методи [toLowerCase()](mdn:js/String/toLowerCase) та [toUpperCase()](mdn:js/String/toUpperCase) змінюють регістр символів:
```js run
-alert( 'Interface'.toUpperCase() ); // INTERFACE
-alert( 'Interface'.toLowerCase() ); // interface
+alert( 'Інтерфейс'.toUpperCase() ); // ІНТЕРФЕЙС
+alert( 'Інтерфейс'.toLowerCase() ); // інтерфейс
```
-Or, if we want a single character lowercased:
+Або якщо ми хочемо перенести в нижній регістр конкретний символ:
```js
-alert( 'Interface'[0].toLowerCase() ); // 'i'
+alert( 'Інтерфейс'[0].toLowerCase() ); // 'і'
```
-## Searching for a substring
+## Пошук підрядка
-There are multiple ways to look for a substring within a string.
+Існує декілька способів для пошуку підрядка.
### str.indexOf
-The first method is [str.indexOf(substr, pos)](mdn:js/String/indexOf).
+Перший метод -- [str.indexOf(substr, pos)](mdn:js/String/indexOf).
-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.
+Він шукає підрядок `substr` в рядку `str`, починаючи з позиції `pos`, і повертає позицію, де знаходиться збіг, або `-1` якщо збігів не було знайдено.
-For instance:
+Наприклад:
```js run
-let str = 'Widget with id';
+let str = 'Віджет з ідентифікатором';
-alert( str.indexOf('Widget') ); // 0, because 'Widget' is found at the beginning
-alert( str.indexOf('widget') ); // -1, not found, the search is case-sensitive
+alert( str.indexOf('Віджет') ); // 0, тому що 'Віджет' було знайдено на початку
+alert( str.indexOf('віджет') ); // -1, збігів не знайдено, пошук чутливий до регістру
-alert( str.indexOf("id") ); // 1, "id" is found at the position 1 (..idget with id)
+alert( str.indexOf("ід") ); // 1, підрядок "ід" знайдено на позиції 1 (..іджет з ідентифікатором)
```
-The optional second parameter allows us to start searching from a given position.
+Необовʼязковий другий параметр `pos` дозволяє нам почати пошук із заданої позиції.
-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`:
+Наприклад, перший збіг `"ід"` знаходиться на позиції `1`. Щоб знайти наступний збіг, почнемо пошук з позиції `2`:
```js run
-let str = 'Widget with id';
+let str = 'Віджет з ідентифікатором';
-alert( str.indexOf('id', 2) ) // 12
+alert( str.indexOf('ід', 2) ) // 9
```
-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:
+Щоб знайти усі збіги, нам потрібно запустити `indexOf` в циклі. Кожен новий виклик здійснюється з позицією після попереднього збігу:
```js run
-let str = 'As sly as a fox, as strong as an ox';
+let str = 'Хитрий, як лисиця, сильний, як Як';
-let target = 'as'; // let's look for it
+let target = 'як'; // давайте знайдемо це
let pos = 0;
while (true) {
let foundPos = str.indexOf(target, pos);
if (foundPos == -1) break;
- alert( `Found at ${foundPos}` );
- pos = foundPos + 1; // continue the search from the next position
+ alert( `Знайдено тут: ${foundPos}` );
+ pos = foundPos + 1; // продовжуємо з наступної позиції
}
```
-The same algorithm can be layed out shorter:
+Той самий алгоритм можна записати коротше:
```js run
-let str = "As sly as a fox, as strong as an ox";
-let target = "as";
+let str = "Хитрий, як лисиця, сильний, як Як";
+let target = "як";
*!*
let pos = -1;
@@ -281,232 +281,232 @@ while ((pos = str.indexOf(target, pos + 1)) != -1) {
```
```smart header="`str.lastIndexOf(substr, position)`"
-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.
+Також є схожий метод [str.lastIndexOf(substr, position)](mdn:js/String/lastIndexOf), що виконує пошук від кінця рядка до його початку.
-It would list the occurrences in the reverse order.
+У ньому будуть перераховані збіги в зворотному порядку.
```
-There is a slight inconvenience with `indexOf` in the `if` test. We can't put it in the `if` like this:
+Існує незручність з `indexOf` в умові `if`. Ми не можемо помістити його в `if` таким чином:
```js run
-let str = "Widget with id";
+let str = "Віджет з ідентифікатором";
-if (str.indexOf("Widget")) {
- alert("We found it"); // doesn't work!
+if (str.indexOf("Віджет")) {
+ alert("Є співпадіння"); // не працює
}
```
-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`.
+В прикладі вище `alert` не відображається, оскільки `str.indexOf("Віджет")` повертає `0` (це означає, що він знайшов збіг у початковій позиції). Це правильно, але `if` вважає, що `0` -- це `false`.
-So, we should actually check for `-1`, like this:
+Тому нам потрібно робити перевірку на `-1`, як тут:
```js run
-let str = "Widget with id";
+let str = "Віджет з ідентифікатором";
*!*
-if (str.indexOf("Widget") != -1) {
+if (str.indexOf("Віджет") != -1) {
*/!*
- alert("We found it"); // works now!
+ alert("Є співпадіння"); // тепер працює!
}
```
-#### The bitwise NOT trick
+#### Трюк з побітовим НЕ
-One of the old tricks used here is the [bitwise NOT](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/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.
+Один зі старих прийомів, який тут використовується, це [побітовий оператор НЕ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_NOT) -- `~`. Він перетворює число в 32-розрядне ціле число (вилучає десяткову частину, якщо вона є), а потім повертає всі біти в його двійковому представленні.
-In practice, that means a simple thing: for 32-bit integers `~n` equals `-(n+1)`.
+На практиці це означає просту річ: для 32-розрядних чисел значення `~n` рівне `-(n+1)`.
-For instance:
+Наприклад:
```js run
-alert( ~2 ); // -3, the same as -(2+1)
-alert( ~1 ); // -2, the same as -(1+1)
-alert( ~0 ); // -1, the same as -(0+1)
+alert( ~2 ); // -3, те саме що -(2+1)
+alert( ~1 ); // -2, те саме що -(1+1)
+alert( ~0 ); // -1, те саме що -(0+1)
*!*
-alert( ~-1 ); // 0, the same as -(-1+1)
+alert( ~-1 ); // 0, те саме що -(-1+1)
*/!*
```
-As we can see, `~n` is zero only if `n == -1` (that's for any 32-bit signed integer `n`).
+Як ми можемо бачити, `~n` рівне 0 лише при `n == -1` (для будь якого 32-розрядного цілого числа `n`)
-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.
+Відповідно, проходження перевірки `if ( ~str.indexOf("...") )` означає, що результат `indexOf` відрізняється від `-1`. Іншими словами, коли є збіг.
-People use it to shorten `indexOf` checks:
+Таку перевірку іноді використовують як компактний `indexOf`:
```js run
-let str = "Widget";
+let str = "Віджет";
-if (~str.indexOf("Widget")) {
- alert( 'Found it!' ); // works
+if (~str.indexOf("Віджет")) {
+ alert( 'Є співпадіння!' ); // Працює
}
```
-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.
+Зазвичай використовувати можливості мови неочевидним способом не рекомендується, але цей трюк широко використовується в старому коді, тому це важливо розуміти.
-Just remember: `if (~str.indexOf(...))` reads as "if found".
+Просто запамʼятайте: `if (~str.indexOf(...))` означає "якщо знайдено".
-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 correct only if a string is not that long.
+Проте, якщо бути точніше, через те, що великі числа обрізаються до 32 бітів оператором `~`, існують числа, для яких результат також буде `0`, найменше таке число `~4294967295=0`. Тому така перевірка буде працювати для рядків невеликої довжини.
-Right now we can see this trick only in the old code, as modern JavaScript provides `.includes` method (see below).
+Зараз такий трюк ми можемо побачити лише в старому коді, тому що в сучасному JavaScript є метод `.includes`.
### includes, startsWith, endsWith
-The more modern method [str.includes(substr, pos)](mdn:js/String/includes) returns `true/false` depending on whether `str` contains `substr` within.
+Сучасніший метод [str.includes(substr, pos)](mdn:js/String/includes) повертає `true/false` в залежності від того чи є `substr` в рядку `str`.
-It's the right choice if we need to test for the match, but don't need its position:
+Цей метод доцільно використовувати, коли потрібно перевірити чи є збіг, але не потрібна позиція:
```js run
-alert( "Widget with id".includes("Widget") ); // true
+alert( "Віджет з ідентифікатором".includes("Віджет") ); // true
-alert( "Hello".includes("Bye") ); // false
+alert( "Привіт".includes("Бувай") ); // false
```
-The optional second argument of `str.includes` is the position to start searching from:
+Необовʼязковий другий аргумент `pos` -- це позиція з якої почнеться пошук:
```js run
-alert( "Widget".includes("id") ); // true
-alert( "Widget".includes("id", 3) ); // false, from position 3 there is no "id"
+alert( "Віджет".includes("ід") ); // true
+alert( "Віджет".includes("ід", 3) ); // false, починаючи з 3-го символа, підрядка "ід" немає
```
-The methods [str.startsWith](mdn:js/String/startsWith) and [str.endsWith](mdn:js/String/endsWith) do exactly what they say:
+Відповідно, методи [str.startsWith](mdn:js/String/startsWith) та [str.endsWith](mdn:js/String/endsWith) перевіряють, чи починається і чи закінчується рядок певним підрядком.
```js run
-alert( "Widget".startsWith("Wid") ); // true, "Widget" starts with "Wid"
-alert( "Widget".endsWith("get") ); // true, "Widget" ends with "get"
+alert( "Віджет".startsWith("Від") ); // true, "Віджет" починається з "Від"
+alert( "Віджет".endsWith("жет") ); // true, "Віджет" закінчується підрядком "жет"
```
-## Getting a substring
+## Отримання підрядка
-There are 3 methods in JavaScript to get a substring: `substring`, `substr` and `slice`.
+В JavaScript є 3 метода для отримання підрядка: `substring`, `substr` та `slice`.
`str.slice(start [, end])`
-: Returns the part of the string from `start` to (but not including) `end`.
+: Повертає частину рядка починаючи від `start` до (але не включно) `end`.
- For instance:
+ Наприклад:
```js run
let str = "stringify";
- alert( str.slice(0, 5) ); // 'strin', the substring from 0 to 5 (not including 5)
- alert( str.slice(0, 1) ); // 's', from 0 to 1, but not including 1, so only character at 0
+ alert( str.slice(0, 5) ); // 'strin', підрядок від 0 до 5 (5 не включно)
+ alert( str.slice(0, 1) ); // 's', від 0 до 1, але 1 не включно, тому лише символ на позиції 0
```
- If there is no second argument, then `slice` goes till the end of the string:
+ Якщо другий аргумент відсутній, тоді `slice` поверне символи до кінця рядка:
```js run
let str = "st*!*ringify*/!*";
- alert( str.slice(2) ); // 'ringify', from the 2nd position till the end
+ alert( str.slice(2) ); // 'ringify', з позиції 2 і до кінця
```
- Negative values for `start/end` are also possible. They mean the position is counted from the string end:
+ Також для `start/end` можна задати відʼємне значення. Це означає, що позиція буде рахуватися з кінця рядка:
```js run
let str = "strin*!*gif*/!*y";
- // start at the 4th position from the right, end at the 1st from the right
+ // починаємо з 4-го символа справа, і закінчуємо на 1-му символі справа
alert( str.slice(-4, -1) ); // 'gif'
```
`str.substring(start [, end])`
-: Returns the part of the string *between* `start` and `end`.
+: Повертає частину рядка *між* `start` та `end`.
- This is almost the same as `slice`, but it allows `start` to be greater than `end`.
+ Цей метод майже такий самий що і `slice`, але він дозволяє задати `start` більше ніж `end`.
- For instance:
+ Наприклад:
```js run
let str = "st*!*ring*/!*ify";
- // these are same for substring
+ // для substring ці два приклади однакові
alert( str.substring(2, 6) ); // "ring"
alert( str.substring(6, 2) ); // "ring"
- // ...but not for slice:
- alert( str.slice(2, 6) ); // "ring" (the same)
- alert( str.slice(6, 2) ); // "" (an empty string)
+ // ...але не для slice:
+ alert( str.slice(2, 6) ); // "ring" (те саме)
+ alert( str.slice(6, 2) ); // "" (порожній рядок)
```
- Negative arguments are (unlike slice) not supported, they are treated as `0`.
+ Відʼємні аргументи (на відміну від slice) не підтримуються, вони інтерпретуються як `0`.
`str.substr(start [, length])`
-: Returns the part of the string from `start`, with the given `length`.
+: Повертає частину рядка з позиції `start`, із заданою довжиною `length`.
- In contrast with the previous methods, this one allows us to specify the `length` instead of the ending position:
+ На відміну від попередніх методів, цей дозволяє вказати довжину `length` замість кінцевої позиції:
```js run
let str = "st*!*ring*/!*ify";
- alert( str.substr(2, 4) ); // 'ring', from the 2nd position get 4 characters
+ alert( str.substr(2, 4) ); // 'ring', починаючи з позиції 2 отримуємо 4 символа
```
- The first argument may be negative, to count from the end:
+ Перший аргумент може бути відʼємним, щоб рахувати з кінця:
```js run
let str = "strin*!*gi*/!*fy";
- alert( str.substr(-4, 2) ); // 'gi', from the 4th position get 2 characters
+ alert( str.substr(-4, 2) ); // 'gi', починаючи з позиції 4 з кінця отримуєму 2 символа
```
-Let's recap these methods to avoid any confusion:
+Давайте підсумуємо ці методи щоб не заплутатись:
-| method | selects... | negatives |
+| Метод | вибирає... | відʼємні значення |
|--------|-----------|-----------|
-| `slice(start, end)` | from `start` to `end` (not including `end`) | allows negatives |
-| `substring(start, end)` | between `start` and `end` | negative values mean `0` |
-| `substr(start, length)` | from `start` get `length` characters | allows negative `start` |
+| `slice(start, end)` | від `start` до `end` (`end` не включно) | дозволяє відʼємні значення |
+| `substring(start, end)` | між `start` та `end` | відʼємні значення інтерпретуються як `0` |
+| `substr(start, length)` | `length` символів від `start` | дозволяє відʼємні значення `start` |
-```smart header="Which one to choose?"
-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.
+```smart header="Який метод вибрати?"
+Усі вони можуть виконати задачу. Формально `substr` має незначний недолік: він описаний не в основній специфікації JavaScript, а в Annex B, який охоплює лише функції браузера, які існують переважно з історичних причин. Тому не браузерні середовища, можуть не підтримувати його. Але на практиці це працює всюди.
-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.
+З двох інших варіантів `slice` дещо гнучкіший, він допускає негативні аргументи та коротший в записі. Отже, достатньо запамʼятати лише `slice` з цих трьох методів.
```
-## Comparing strings
+## Порівняння рядків
-As we know from the chapter Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: