Posts.

Mastering JavaScript | 8 STRING METHODS | PART 3

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

Welcome back to our "Master JavaScript" course! In this video, we'll continue our exploration of essential JavaScript string methods. As we dive further into the world of strings, you'll gain a deeper understanding of how to effectively manipulate and transform text data.

Let's pick up, where we left off and cover more string methods.

indexOf() and lastIndexOf() The indexOf() method helps you find the first occurrence of a specified substring within a string. while lastIndexOf() does the same but starts searching from the end of the string:

1const text = "Hello, World!"; 2const firstIndex = text.indexOf("o"); 3console.log(firstIndex);

The includes() method checks whether a substring is present within a string, returning a Boolean value:

1const text = "Hello, World!"; 2const includesHello = text.includes("Hello"); 3console.log(includesHello); // Output: true

startsWith() and endsWith()

To check if a string starts or ends with a specific substring, use the startsWith() and endsWith() methods:

1const text = "Hello, World!"; 2const startsWithHello = text.startsWith("Hello"); 3console.log(startsWithHello); // Output: true 4const endsWithWorld = text.endsWith("World!"); 5console.log(endsWithWorld); // Output: true


match()

The match() method uses a regular expression to search for matches in a string, returning an array of matches:

1const text = "Hello, John and Jane!"; 2const regex = /[A-Z][a-z]+/g; 3const matches = text.match(regex); 4console.log(matches); // Output: ["Hello", "John", "Jane"]

matchAll()

The matchAll() method is used to find all matches of a regular expression in a string. Unlike match(), which returns an array of matches, matchAll() returns an iterator that allows you to iterate through all matches and their capture groups.

1const text = "Hello, John and Jane!"; 2const regex = /[A-Z][a-z]+/g; 3const matchesIterator = text.matchAll(regex); 4for (const match of matchesIterator) { 5 console.log(`Full match: ${match[0]}`); 6} 7/* output 8 Full match: Hello 9 Full match: John 10 Full match: Jane 11*/

repeat()

the final method in this video is the repeat() method is used to create a new string by repeating the original string a specified number of times.

1const text = "Hello!"; 2const repeatedText = text.repeat(3); 3console.log(repeatedText); // Output: "Hello!Hello!Hello!"

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