How to split String to character array using JavaScript

In this article, you will how to split string into an array using JavaScript. There are multiple ways to convert JavaScript string to an array of either characters or even substrings.

Convert String to Array using split() method:

You can use the split() method in JavaScript to convert a string into an array of characters. The split() method returns an array of substrings after splitting the given string by a separator given by you.

Syntax:

str = "some string is here"
str.split(separator, limit)

Parameters

separator: The pattern describing where each split should occur

limit (Optional): A non-negative integer specifying a limit on the number of substrings to be included in the array. The array may contain fewer entries than limit if the end of the string is reached before the limit is reached
i-e If limit is 0, [] is returned.

let str = "Hello, World!"; 
let charArray = str.split('');
console.log(charArray);

Convert comma  separator string to array using split() method:

To convert a comma-separated string to an array using the split() method, you can use the following code:

let string = "apple,banana,orange";
let array = string.split(",");
console.log(array); // Output: ["apple", "banana", "orange"]

The split() method takes a separator as an argument and splits the string into an array of substrings based on that separator. In this case, the separator is a comma (,), so the string is split into an array of three substrings: "apple", "banana", and "orange".


Use regular Expression (Regex) to split a string:

 The regex is a very powerful tool and can be used as a separator to split string to array of substrings 

let string = 'Hello! how are you? whats happening.';
let arrayfromstring = string.split(/[!,?,.]/);
console.log( arrayfromstring );


Use array.from() method to convert a string to array:
Alternatively, you can use Array.from() method to convert a string into an array of characters. The Array.from() method creates a new, shallow-copied Array instance from an array-like or iterrable object.

If you want to split the string on a specific separator and convert it to array you can pass the string and separator as arguments to Array.from() method as follows

let string = "apple,banana,orange";
let array = Array.from(string.split(','));
console.log(array); // ["apple", "banana", "orange"]

 

Use spread operator `...` method to convert a string to array:

The spread operator `(...)` is a feature in JavaScript that allows you to expand an iterable (such as an array or a string) into individual elements.

let string = "apple";
let array = [...string];
console.log(array); //["a", "p", "p", "l", "e"]

You can also use the spread operator to split a string on a specific separator and convert it to an array.

In this case, the spread operator takes the output of the split() method, which is an array of substrings, and expands it into an array of individual elements.

let string = "apple,banana,orange";
let array = [...string.split(',')];
console.log(array); // ["apple", "banana", "orange"]

 

Thanks:


Tags:

Share:

Related posts