This post will present a comprehensive overview of the below-listed methods to get a character array from a string in JavaScript:
So, let’s begin!
string.split() function in JavaScript
split() is a built-in function in JavaScript that splits/breaks a string into substrings(words or characters). It produces a new modified array without affecting the given array.
Syntax
The below snippet will assist you to understand the syntax of the array.split() method:
Both separator and limit are optional parameters. A string/regular expression can be passed as a separator to the split() method. If we didn’t specify any parameter then the split() method will return the original string.
Example: Get a character array using the split() method
In this example program, we will understand how to use the split() method to get an array of characters from the given string:
var resultantArray = originalString.split("");
console.log("Resultant Array: ", resultantArray);
In this program, we passed “” as a separator to the split method. Consequently, the split() method will break the given string into characters and it will return an array of characters as shown in the below-given snippet:
This is how we can get a character array from a string in JavaScript using the split method.
substring() function in JavaScript
The substring() method in JavaScript is used to extract a certain number of characters/letters from the given string. The substring() function extracts the characters between two positions and returns a new modified array without affecting the original array.
Syntax
The below snippet will explain the basic syntax of the substring() method:
Here, the initial_index is a required parameter that specifies the starting position while the final_index is an optional parameter that specifies the end position. Omitting the second parameter, i.e. final_index will return the rest of the string.
Example: Get a character array using the substring() method
In this program, we will learn how to use the substring() function to get an array of characters from the given string:
var newArray = [];
for (i = 0; i < originalString.length; i++) {
newArray[i] = originalString.substring(i, i + 1);
}
console.log("Resultant Array: ", newArray);
In this program, we utilized the substring() method to get the characters from the given string and store them in an array:
In this way, we can utilize the substring() method to get the array from the given string in JavaScript.
substring() function in JavaScript
It is a built-in static method in JavaScript that returns a new array from any iterable object. In simple words, the from() method enables us to create a new array from iterable objects or array-like objects.
Syntax
The basic syntax of the from() method will be something like this:
Here, the object parameter represents an object to be converted to an array. The mapFun parameter represents a call-back function that will be invoked on each element while thisArg is a value that is used as this value for the map function. The object is a compulsory parameter while mapFun and thisArg are optional parameters.
Example: Get a character array using the from() method
In this program, we will learn how to use the substring() function to get an array of characters from the given string:
var newArray = Array.from(originalString);
console.log("Resultant Array: ", newArray);
In this example, we passed the “originalString” to the “Array.from()” method to get a character array from the given string:
This is how the Array.from() method works in JavaScript.
Conclusion
In JavaScript, different methods such as Array.from(), string.substring(), string.split() etc. are used to get an array of characters from the given string. The split() method splits the given string into substrings, the substring() function extracts a specific number of characters from the given string, and from() method returns a new array from any iterable object. This post explained different methods to get an array of characters from the given string in JavaScript.
from https://ift.tt/JOPsMat
0 Comments