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.
1const message = "Hello, World!";
2console.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:
1const message = "Hello, World!";
2const sliced = message.slice(0, 5);
3console.log(sliced); // Output: "Hello"
3. String substring()
The substring()
method is quite similar to slice()
, but it handles negative indices differently.
1const message = "Hello, World!";
2const substringed = message.substring(7, 12);
3console.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:
1const message = "Hello, World!";
2const substrResult = message.substr(7, 5);
3console.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:
1const message = "Hello, World!";
2const replaced = message.replace("Hello", "Hi");
3console.log(replaced); // Output: "Hi, World!"
6. String replaceAll()
Similar to replace()
, the replaceAll()
method replaces all occurrences of a specified value or regular expression:
1const message = "Hello, World!";
2const replacedAll = message.replaceAll("o", "a");
3console.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:
1const message = "Hello, World!";
2const upperCaseMessage = message.toUpperCase();
3console.log(upperCaseMessage); // Output: "HELLO, WORLD!"
4const lowerCaseMessage = message.toLowerCase();
5console.log(lowerCaseMessage); // Output: "hello, world!"
9. String concat()
Finally, we have the concat()
method, which lets us combine multiple strings into a single string:
1const firstName = "John";
2const lastName = "Doe";
3const fullName = firstName.concat(" ", lastName);
4console.log(fullName); // Output: "John Doe"
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!
485 words - 2,754 characters