Without further delay, let's jump right in.
Now that you've learned the theory behind methods and strings let's dive into how they work together.
Check out this video on string methods to learn more about what they are.
As an example, one use of strings is to sterilize any malicious code from being injected into a program or piece of software. This process happens using a regex — or regular expression — which in no way looks like regular expressions.
Regular expressions are patterns on strings to ensure they meet specific formatting requirements. Enough on the theory behind methods, now let’s dive into some code examples.
Truthfully, using string methods is relatively easy to do. However, understanding what's happening is a different topic. Let’s clarify this with an example, using a string method is simply a matter of understanding the syntax. Let’s look at a code example of the syntax below.
let str = “Doctor Who”; str.someRandomMethod();
That is the bare bones of literally every string method, it starts with a variable holding a primitive string value. Then you chain the desired method to the string variable using the dot notation and finish it off with parenthesis and a semicolon. Here is a code snippet showing how to use the trim() method.
let str = “Yes, but Doctor Who?”; str.trim(); /*Would return the string “Yes,butDoctorWho?”*/
Simple enough, right? Well, what if that method requires a parameter? In that case, the method will accept some criteria on what to do with the string value. This behavior changes things a little, let's look at a simple example of a method with parameters. The code below does the same as the code above, removing any whitespace from a string. Only this time it's done using the replace() method.
The replace method accepts two parameters, the first is what the string method should look for, and the second is the value to replace with the target.
let str = “I’m a Timelord from the planet Gallifrey.”; str.replace(/\s/g,'');\ /*Would return the string “I’maTimelordfromtheplanetGallifrey.”*/
Assuming we use the following string value assigned to a variable as our target string, the methods would look like the following examples.
let str = “I’m a Timelord from the planet Gallifrey.”;
str.toLowerCase(); /*Would return the string, "i’m a timelord from the planet gallifrey."*/
let num = 904; num.toString(); /*Would return “904”*/
str.toUpperCase(); /*Would return the string, "I’M A TIMELORD FROM THE PLANET GALLIFREY."*/
str.substring(31, 40); /*Would return the string “Gallifrey”. NOTE: Excluding the second index will return all characters after the start index.*/
str.match("Timelord"); /*Would return an array object with the matched value. Regular expressions can also be used.*/
str.concat(” I’m The Doctor, look me up!”); /*Would return “I’m a Timelord from the planet Gallifrey. I’m The Doctor, look me up!”*/
This list is by no means exhaustive, there are many more methods, and some are much less known than others. An example of these lesser-known methods would be the padStart() and padEnd() methods which allow you to add a string to the beginning and end of a string value.
Getting Started Using JavScript String Methods
There is a lot to learn under the umbrella of strings and string methods, the best way to solidify all the information is to practice using them. There are an endless number of applications that use strings, and even more ways methods can help.
Working with string methods, you can learn to understand the boundaries and behaviors and how to use methods together. By combining string methods, you can perform powerful tasks and do almost anything your software may require.
Originally published Jun 28, 2022 6:00:00 AM, updated June 28 2022