Array to string and String to arrays

Modified on Mon, 1 Jul at 1:20 PM

Split Method

The `.split()` method splits a string into an array of strings based on a specified separator.


Syntax:

str.split(separator)
  • `separator`: The separator on which to split the string.


Choosing a Separator

Example 1: Splitting into Characters

To split the string “test” into an array of characters:

let str = "test";

let arr = str.split(“”);

console.log(arr); // [“t”, “e”, “s”, “t”]

Using an empty string as the separator, each character in the string “test” is treated as a separate element in the resulting array.


Example 2: Splitting into Words

To split the string “String to Array” into an array of words:

let str = "String to Array";

let arr = str.split(“ “);

console.log(arr); // [“String”, “to”, “Array”]

Here, the whitespace(“ ”) character is used as the separator, so the string is split at each space, resulting in an array where each word is an element.


Example 3: Splitting a List of Names

We can split a string of comma and space separated names “Bret, Andy, Samantha” using a comma followed by a space as the separator:

let str = "Bret, Andy, Samantha";

let arr = str.split(“, “);

console.log(arr); // [“Bret”, “Andy”, “Samantha”]


The comma and space combination is used as the separator, so the string is split at each occurrence of `, `, resulting in an array of names.


Join Method

The `.join()` method does the opposite of the `.split()` method. It joins an array of strings to a string joined by the separator.


Syntax:

arr.join(separator)
  • `separator`: The separator on which to join the array elements.


Choosing a Separator

Example 1: Adding Characters

To create a word from the array `[“t”, “e”, "s", "t"]`:

let arr = ["t", "e", "s", "t"]; 

let str = arr.join(“”);

console.log(str); // "test"

Using an empty string (“”) as the separator, the characters in the array are joined without any additional characters, forming the word “test”.


Example 2: Adding Words

To create a sentence from the array of words `["String", "to", "Array"]`:


let arr = ["String", "to", "Array"]

let str = arr.join(" ");

console.log(str); // "String to Array"

The whitespace (“ “) is used as the separator, so the words in the array are joined with a space between each, forming the sentence “String to Array”.


Example 3: Creating a String Separated by Comma

To join an array of names into a comma-separated string:


let arr = ["Bret", "Andy", "Samantha"];

let str = arr.join(", ");

console.log(str); // "Bret, Andy, Samantha"

The comma and space combination is used as the separator, so the names in the array are joined with “, “ between each, forming the string “Bret, Andy, Samantha”.


Conclusion

Using the `.split()` and `.join()` methods allows you to convert strings into arrays and vice versa easily. By understanding the basics of string splitting and joining, you can ensure your code is robust and reliable.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article