In this article, we will learn to split a string into substrings in JavaScript. Let’s start by defining a string. A string is any sequence of characters enclosed within quotation marks. A string can be a sentence, a phrase, or even a single word. In JavaScript, strings are represented as an array of characters. To demonstrate this, we will start by creating a string.
Step 1: Create a string by entering the following code:
var str = “JavaScript is a programming language that runs in browsers. It has a simple, fast implementation and is easy to learn.”;
Step 2: To access the first character in a string, we will use the index notation. The index starts at zero and is incremented by one each time we access a character. For example, the second character in the string we just created is “JavaScript.”
var str = “JavaScript is a programming language that runs in browsers. It has a simple, fast implementation and is easy to learn.”;
str[0] = ‘J’; // This is the letter J.
str[1] = ‘a’; // This is the letter A.
str[2] = ‘v’; // This is the letter V.
Step 3: We can use the index notation again to access the second character in the string.
str[1] = ‘a’; // This is the letter A.
str[2] = ‘v’; // This is the letter V.
Step 4: We can access any character in the string by adding the value of the index plus one to the beginning of the string. The character at the end of the string is accessed by subtracting one from the index.
str[1] = ‘a’; // This is the letter A.
str[2] = ‘v’; // This is the letter V.
str[3] = ‘e’; // This is the letter E.
Step 5: We can add a string to another string. The first character in the new string is the same as the first character in the original string, and the rest of the new string is appended to the original.
str[0] = ‘J’; // This is the letter J.
str[1] = ‘a’; // This is the letter A.
str[2] = ‘v’; // This is the letter V.
str[3] = ‘e’; // This is the letter E.
str[4] = ‘c’; // This is the letter C.
Step 6: We can also remove characters from a string. If the character is not found, the character is removed from the string.
str.substring(0,1); // This is the letter J.
Step 7: We can split a string into substrings by using the substring method. This method returns a new string made up of a specified number of characters starting from a specified index. The length of the new string is equal to the number of characters in the original string minus the number of characters we specify.
var str = “JavaScript is a programming language that runs in browsers. It has a simple, fast implementation and is easy to learn.”;
var subStrings = str.substring(0,3); // This is the letter J.
var subStrings = str.substring(1,2); // This is the letter JavaScript.
Step 8: We can use the substring method to split a string into multiple substrings.
var str = “JavaScript is a programming language that runs in browsers. It has a simple, fast implementation and is easy to learn.”;
var subStrings = str.substring(0,3); // This is the letter J.
var subStrings = str.substring(1,2); // This is the letter JavaScript.
var subStrings = str.substring(2,4); // This is the letter is.
var subStrings = str.substring(3,5); // This is the letter a.