Posts.

Mastering JavaScript | STRING METHODS | part 1

Cover Image for Mastering JavaScript |  STRING METHODS | part 1
Rami Al-Karo
Rami Al-Karo

Welcome back to our "Master JavaScript" course! In today's video, we'll delve into the powerful world of JavaScript string methods. Strings are an essential part of any programming language, and JavaScript offers a wide array of methods to help us manipulate and search within strings without effort.

We'll explore in this part, some of the most commonly used string methods and provide examples to demonstrate how they work. So, let's get started!


1. String length The first method we'll look at is length. It allows us to find the number of characters in a string.

1 2 const message = "Hello, World!"; console.log(message.length); // Output: 13

2. String slice() Next up, we have the slice() method. It extracts a portion of a string and returns a new string. By providing the starting index (inclusive) and the ending index (exclusive), we can select the desired substring:

1 2 3 const message = "Hello, World!"; const sliced = message.slice(0, 5); console.log(sliced); // Output: "Hello"


3. String substring() The substring() method is quite similar to slice(), but it handles negative indices differently.

1 2 3 const message = "Hello, World!"; const substringed = message.substring(7, 12); console.log(substringed); // Output: "World"


4. String substr() Now, we have the substr() method is also similar to slice(). It extracts a substring starting from a specific index and extending for the specified length:

1 2 3 const message = "Hello, World!"; const substrResult = message.substr(7, 5); console.log(substrResult); // Output: "World"


5. String replace() The replace() method searches for a specified value or regular expression in a string and replaces it with a new value. By default, it replaces only the first case:

1 2 3 const message = "Hello, World!"; const replaced = message.replace("Hello", "Hi"); console.log(replaced); // Output: "Hi, World!"
1

6. String replaceAll() Similar to replace(), the replaceAll() method replaces all occurrences of a specified value or regular expression:

1 2 3 const message = "Hello, World!"; const replacedAll = message.replaceAll("o", "a"); console.log(replacedAll); // Output: "Hella, Warld!"


7. String toUpperCase() and 8.String toLowerCase() These methods allow us to convert a string to either uppercase or lowercase:

1 2 3 4 5 const message = "Hello, World!"; const upperCaseMessage = message.toUpperCase(); console.log(upperCaseMessage); // Output: "HELLO, WORLD!" const lowerCaseMessage = message.toLowerCase(); console.log(lowerCaseMessage); // Output: "hello, world!"


9. String concat() Finally, we have the concat() method, which lets us combine multiple strings into a single string:

1 2 3 4 const firstName = "John"; const lastName = "Doe"; const fullName = firstName.concat(" ", lastName); console.log(fullName); // Output: "John Doe"


Conclusion

You've now mastered some of the most commonly used JavaScript string methods. Understanding and using these methods will greatly enhance your ability to manipulate and search within strings in your JavaScript programs. If you found this video helpful, don't forget to give it a thumbs up and subscribe to our channel. Happy coding, and see you in the next video!


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