Рядки — незмінні (immutable). Всі методи повертають новий рядок.
1class="hl-keyword">const s = class="hl-string">039;Hello, World!039;;
2
3s.length class="hl-comment">// 13
4s.toUpperCase() class="hl-comment">// class="hl-string">039;HELLO, WORLD!039;
5s.toLowerCase() class="hl-comment">// class="hl-string">039;hello, world!039;
6s.trim() class="hl-comment">// прибирає пробіли з країв
7s.trimStart() class="hl-comment">// тільки зліва
8s.trimEnd() class="hl-comment">// тільки справа
9
10s.includes(class="hl-string">039;World039;) class="hl-comment">// true
11s.startsWith(class="hl-string">039;Hello039;) class="hl-comment">// true
12s.endsWith(class="hl-string">039;!039;) class="hl-comment">// true
13s.indexOf(class="hl-string">039;o039;) class="hl-comment">// 4 (перше входження)
14s.lastIndexOf(class="hl-string">039;o039;) class="hl-comment">// 8
15
16s.slice(7, 12) class="hl-comment">// class="hl-string">039;World039;
17s.slice(-6) class="hl-comment">// class="hl-string">039;orld!039;
18s.substring(0, 5) class="hl-comment">// class="hl-string">039;Hello039;
19s.replace(class="hl-string">039;World039;, class="hl-string">039;JS039;) class="hl-comment">// class="hl-string">039;Hello, JS!039;
20s.replaceAll(class="hl-string">039;l039;, class="hl-string">039;L039;) class="hl-comment">// class="hl-string">039;HeLLo, WorLd!039;
21s.split(class="hl-string">039;, 039;) class="hl-comment">// [class="hl-string">039;Hello039;, class="hl-string">039;World!039;]
22s.repeat(2) class="hl-comment">// class="hl-string">039;Hello, World!Hello, World!039;'42'.padStart(5, '0') // '00042'
'hi'.padEnd(5, '.') // 'hi...'
const s = ' JavaScript ';
const trimmed = s.trim();
console.log(trimmed);
console.log(trimmed.length);
console.log(trimmed.toUpperCase());
const email = 'user@Example.COM';
console.log(email.toLowerCase());
console.log(email.includes('@'));
console.log(email.toLowerCase().replace('example', 'gmail'));
const data = 'Іван,25,Київ,розробник';
const parts = data.split(',');
console.log(`Ім'я: ${parts[0]}`);
console.log(`Вік: ${parts[1]}`);
console.log(`Місто: ${parts[2]}`);
console.log(`Професія: ${parts[3]}`);