This guide will explore the process of verifying substrings in JavaScript. Initially, let's grasp the concepts of strings and substrings.
String
In programming, a string is a sequence of characters that is used to store and manipulate text data. In JavaScript, a string is a fundamental data type that can include symbols, spaces, letters, and numbers. Strings in JavaScript must always be enclosed within quotation marks.
Example:
"You are welcome!"
Substring
A substring refers to a segment of characters within a larger string. For example, in the string "You are welcome!", "welcome" is a substring. In JavaScript, the substring function is built-in and is employed to extract a section of a string, creating a smaller string from a larger one.
In JavaScript, strings are immutable, indicating that the original string cannot be changed. Instead, when modifications are made to a string, a new string is created, leaving the original string unchanged.
It consistently provides a substring, which can be a single word or a group of words.
Syntax:
substring(int start_index, int end_index)
In the above-given syntax, substring is a method which consists of two parameters as follows:
- startindex: The first parameter is the startindex which represents the starting index number from where you want to extract a substring.
- endindex: The second parameter is the endindex which represents the ending index number till where you want to extract a substring.
- The substring method returns a string from startindex to endindex .
We can check substring in JavaScript in the following ways:
- Utilization of the includes method to check a substring
- Utilization of the test method to check a substring
- Utilization of the match method to check a substring
- Utilization of the indexOf method to check a substring
- Utilization of the search method to check a substring
Let's carefully examine each technique individually.
Utilization of the includes method to check a substring
The includes function is employed to verify the presence of a specific substring within a string. If the specified substring is found within the string, the method will return the Boolean value "true"; if not, it will return "false".
Syntax:
string.includes(search_value, start)
The includes function has two arguments as described:
-
- : This is the value we are looking for during the search operation.
-
- : It determines the beginning position for the search process. This parameter is not mandatory, and if not specified, the search will start from the beginning (index 0).
Demo:
In JavaScript, we can verify the presence of a substring by using the includes method.
Code:
const str = "It is a shiny day.";
const sub_string = "shiny";
console.log(str.includes(sub_string));
Output:
The method includes is utilized to search for the presence of the substring "shiny" within the string "It is a shiny day." Upon execution, the includes method will yield a Boolean value of "true" indicating that the substring "shiny" exists within the string.
Utilization of the test method to check a substring
The test function is employed to verify the specified sub-string within a string. It will return true if the sub-string is found, otherwise it will return false.
Syntax:
/substring/.test(string)
The test function accepts a single argument, the initial string. The target substring for searching is specified within the forward slashes.
Demo:
In JavaScript, we will examine a substring by using the test function.
Code:
let original_string = "Hi there! Welcome to this website.";
let ss = /website/.test(original_string);
console.log(ss);
Output:
Within the provided code snippet, the function test is utilized to verify the presence of the substring "website" within the string "Hi there! Welcome to this website." Upon inspection of the output, it is evident that the test function yields a result of "true", indicating that the substring "website" exists within the primary string.
Utilization of the match method to check a substring
The match function is employed to search for a specified substring within a string. It returns true if a match is found.
Syntax:
string.match(regExp)
The method match accepts a single argument, which is the regular expression.
Demo:
To discover a substring in JavaScript, we will utilize the match function.
Code:
let original_str = "Hello there! Enjoy your day.";
let sub_string = "Enjoy";
let containsSubstring = original_str.match(new RegExp(sub_string)) !== null;
console.log(containsSubstring);
Output:
In the provided code snippet, the match function is used to search for the occurrence of the substring "Enjoy" within the main string "Hello there! Enjoy your day." Upon execution, the match method confirms the presence of the substring and returns a boolean value of "true".
Utilization of the indexOf method to check a substring
The indexOf function is employed to search for a specific substring within a given string. When the substring is located, the function returns the index of its first occurrence. If the substring is not found within the string, the function returns -1.
Syntax:
string. indexOf(search_value, index);
The indexOf function takes two parameters, which are explained in detail as follows:
The search_value refers to the specific value that will be sought after during the search process.
The starting point specifies where the search should begin. This parameter is not mandatory, and if not provided, the search will start from the default value of 0.
Demo:
In JavaScript, we can search for a substring by employing the indexOf function.
Code:
const org_str = "Hello world! Have an amazing day.";
const sub_str = "amazing";
console.log(org_str.indexOf(sub_str));
Output:
When the indexOf method is used to search for the substring "amazing" within the string "Hello world! Have an amazing day.", it indicates the position of the substring in the original string. By observing the output, we can confirm that the indexOf method identifies the presence of the substring "amazing" in the original string.
Utilization of the search method to check a substring
The search function is employed to look for a specified substring within a string. It will return true if the substring is found.
Syntax:
string.search(new regExp)
The search function takes a single argument, which is the updated regular expression.
Demo:
In JavaScript, we can locate a substring by employing the search function.
Code:
let original_str = "Hi there! Have a good day."
let sub_string = "good";
let result = original_str.search(new RegExp(sub_string)) !== -1;
console.log(result);
Output:
By utilizing the search function, we can verify the presence of the substring "good" within the string "Hi there! Have a good day." The outcome of the search operation distinctly indicates the return of the boolean value "true".
Conclusion:
In this tutorial, we explored the process of verifying substrings in JavaScript. We've learned about different techniques for detecting the existence of a substring within a string, including the deployment of methods like includes, test, match, indexOf, and search.