JavaScript String Methods
JavaScript String Methods
As someone who has worked with JavaScript for a while, I have found that one of the most important things to know are the various String methods available. These methods allow you to manipulate and modify strings in various ways.
Common String Methods
charAt()
: returns the character at a specified index in a stringconcat()
: combines two or more strings into one stringincludes()
: checks whether a string contains a specified substringindexOf()
: returns the index of the first occurrence of a specified value in a stringlastIndexOf()
: returns the index of the last occurrence of a specified value in a stringreplace()
: replaces a specified value with another value in a stringsubstring()
: returns the part of a string between two indexestoLowerCase()
: returns the string in lowercase letterstoUpperCase()
: returns the string in uppercase letterstrim()
: removes whitespace from both sides of a string
Let's take a look at an example of using some of these methods:
let phrase = "The quick brown fox jumps over the lazy dog";
console.log(phrase.charAt(0)); // Output: "T"
console.log(phrase.concat(" and the cat")); // Output: "The quick brown fox jumps over the lazy dog and the cat"
console.log(phrase.includes("fox")); // Output: true
console.log(phrase.indexOf("the")); // Output: 31
console.log(phrase.lastIndexOf("the")); // Output: 31
console.log(phrase.replace("fox", "kangaroo")); // Output: "The quick brown kangaroo jumps over the lazy dog"
console.log(phrase.substring(4, 9)); // Output: "quick"
console.log(phrase.toLowerCase()); // Output: "the quick brown fox jumps over the lazy dog"
console.log(phrase.toUpperCase()); // Output: "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG"
console.log(" hello ".trim()); // Output: "hello"
Regular Expressions
Another powerful tool for working with strings in JavaScript is regular expressions. Regular expressions are patterns that can be used to match and manipulate strings in various ways.
Here are some common methods for working with regular expressions:
test()
: tests whether a string matches a regular expressionmatch()
: searches a string for a regular expression and returns the matched textreplace()
: replaces a specified value with another value in a string, using a regular expression
Here's an example of using regular expressions to replace all instances of the word "fox" in our previous example:
let phrase = "The quick brown fox jumps over the lazy dog";
console.log(phrase.replace(/fox/g, "kangaroo")); // Output: "The quick brown kangaroo jumps over the lazy dog"
In this example, we use the /fox/g
regular expression pattern to replace all instances of the word "fox". The g
flag tells JavaScript to perform a global search, rather than just replacing the first instance.
Regular expressions can be complex, but they provide a lot of power and flexibility for working with strings. If you're interested in learning more, there are many resources available online to help you get started.