Posts.

Mastering JavaScript | 8 STRING METHODS | PART 4

Cover Image for Mastering JavaScript | 8 STRING METHODS | PART 4
Rami Al-Karo
Rami Al-Karo

Hey and welcome to fourth and final part of string methods series, which belong to the Mastering javascript course. Check other parts in channel if you have not yet. let's uncover the remaining string methods that will round out your expertise:


localeCompare()

The localeCompare() method compares two strings based on their locale (language-specific rules) and returns a value indicating their order:

1 2 3 4 const string1 = "apple"; const string2 = "banana"; const result = string1.localeCompare(string2); console.log(result); // Output: -1 (string1 comes before string2)

Return value A negative number if referenceStr occurs before compareString; positive if the referenceStr occurs after compareString; 0 if they are equivalent.


codePointAt()

The codePointAt() method returns the Unicode code point value of the character at a specified index:

1 2 3 const text = "🚀 Launch!"; const codePoint = text.codePointAt(0); console.log(codePoint); // Output: 128640 (Unicode code point of "🚀")


toLocaleUpperCase() and toLocaleLowerCase()

These methods are similar to toUpperCase() and toLowerCase(), but they convert the string to uppercase or lowercase based on the locale:

1 2 3 4 5 const text = "Istanbul"; const upperCaseText = text.toLocaleUpperCase("tr-TR"); // Turkish locale const lowerCaseText = text.toLocaleLowerCase("de-DE"); // German locale console.log(upperCaseText); // Output: Ä°STANBUL console.log(lowerCaseText); // Output: istanbul


search()

The search() method searches for a specified substring or regular expression within a string and returns the index of the first match:

1 2 3 const text = "Hello, World!"; const index = text.search("World"); console.log(index); // Output: 7


normalize()

The nomalize() method is used to normalize Unicode strings. It allows to convert different representations of the same character into a single, standardized form. This can help with string comparison, searching, and processing.

1 2 3 const text = "Café"; const normalizedText = text.normalize(); console.log(normalizedText); // Output: "Café" (normalized form)

toWellFormed()

The toWellFormed() method returns a string where all lone surrogates of this string are replaced with the Unicode replacement character U+FFFD.

1 2 3 4 5 6 7 8 const illFormed = "<https://example.com/search?q=\\uD800>"; try { encodeURI(illFormed); } catch (e) { console.log(e); // URIError: URI malformed } console.log(encodeURI(illFormed.toWellFormed())); // "<https://example.com/search?q=%EF%BF%BD>"


at()

Final method is the at() method is used to access the character at a specific position within a string.

1 2 3 const text = "Hello, World!"; const character = text.at(7); // Accessing character at index 7 (W) console.log(character); // Output: "W"


at the end I’d like to thank you and to say that you've now expanded your knowledge of JavaScript string methods! If you found this video helpful, remember to give it a thumbs up and stay tuned for more content and subscribe. Happy coding, and see you in the next one!


More Stories

Cover Image for  5 Array Methods in JavaScript | Part 3 | Mastering JavaScript

5 Array Methods in JavaScript | Part 3 | Mastering JavaScript

485 words - 2,754 characters

Rami Al-Karo
Rami Al-Karo
Cover Image for 5 Array Methods | Part 2 | Mastering JavaScript

5 Array Methods | Part 2 | Mastering JavaScript

907 words - 5,716 characters

Rami Al-Karo
Rami Al-Karo