Transforming strings
In the previous lesson we cut pieces out of a string and searched it for words. Now let's go back to the copier. A good copier has settings: it can print everything in capital letters, trim empty margins, cut a sheet into strips, or glue strips into one sheet.
These methods are needed when text arrives "dirty". A user typed a name with spaces at the ends, typed a city in capital letters, and sent a list of colors as one comma-separated string. Let's look at the methods that tidy such text up:
const raw = " Ann Lee ";
console.log(raw.trim().toLowerCase()); // ann lee
const parts = "red,green,blue".split(",");
console.log(parts, parts.length); // [ 'red', 'green', 'blue' ] 3
console.log(parts.join(" + ")); // red + green + blue
console.log("ab".repeat(3)); // abababEach of these methods, like slice, leaves the original string as it was and returns a new value. Let's go through them in groups.
Case and spaces
toUpperCase and toLowerCase
toUpperCase makes every letter capital, and toLowerCase makes every letter small. Digits, spaces and punctuation stay as they were. The methods understand Russian letters too: "Ёж".toUpperCase() gives "ЁЖ".
trim
trim removes the spaces and line breaks at both ends of a string. Spaces between words stay: " Ann Lee " becomes "Ann Lee".
Method chaining
Each of these methods returns a string, so you can call the next method on the result right away. This is called method chaining. raw.trim().toLowerCase() works in two steps:
raw.trim()removes the spaces at the ends and gives"Ann Lee"..toLowerCase()is called on that new string and gives"ann lee".
Case-insensitive search
Searching tells capital and small letters apart. To keep case out of the way, both strings are first brought to one case, and only then compared or searched:
const title = "Hello World";
console.log(title.includes("world")); // false
console.log(title.toLowerCase().includes("world")); // true
console.log("ANN".toLowerCase() === "ann"); // trueThe first output line is false: the title spells the word with a capital W. In the second line, toLowerCase first turns the title into "hello world", and the word is found.
Cut and glue
split
split cuts a string at a separator — the substring it looks for between the parts. The result is an array of the parts, and the separator itself does not go into any part. An array has a length — its number of elements.
Node.js prints a whole array in square brackets, and the strings in it in single quotes.
Here the copier analogy falls short. A copier always prints a sheet, while split gives back not a string but an array of several strings.
join
join is an array method, and it does the reverse. It glues the elements into one string and puts the given string between them: parts.join(" + ") gives "red + green + blue".
repeat
repeat(n) returns the string repeated n times: "ab".repeat(3) gives "ababab".
Common mistake
A method is called, but its result is not saved anywhere:
const city = " Paris ";
city.trim(); // ✗
console.log(`[${city}]`);The program prints [ Paris ]: the spaces are still there. trim returned a new string "Paris", but nothing took it, and it was lost. Save the result: const clean = city.trim(); — then clean holds "Paris".
Key points
toUpperCaseandtoLowerCasechange the case,trimremoves the spaces at the ends, and method chaining does it in one expression.- To search or compare ignoring case, first bring both strings to one case.
splitcuts a string at a separator into an array,joinglues an array into a string,repeat(n)repeats a stringntimes.- Methods do not change the original string: save the result in a variable.
Subscribe to take the course
The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.