Navigate back to the homepage

how to explode string in javascript?

Infinitbility
JavaScript
June 26th, 2022 · 1 min read
how to explode string in javascript?

Hi Friends 👋,

Welcome To Infinitbility! ❤️

To explode string in javascript, use split() method it will explode your string based upon your parameter you have passed in a split() method. let’s take a example

The split() method divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array. The division is done by searching for a pattern; where the pattern is provided as the first parameter in the method’s call.

In the following example, we can able explode or split string by every char, space, and your desired string.

1const str = 'Hear about the new restaurant called Karma, There’s no menu: You get what you deserve';
2
3// explode by every char
4str.split('') // ['H', 'e', 'a', 'r', ' ', 'a', 'b', 'o', 'u', 't', ' ', 't', 'h', 'e', ' ', 'n', 'e', 'w', ' ', 'r', 'e', 's', 't', 'a', 'u', 'r', 'a', 'n', 't', ' ', 'c', 'a', 'l', 'l', 'e', 'd', ' ', 'K', 'a', 'r', 'm', 'a', ',', ' ', 'T', 'h', 'e', 'r', 'e', '’', 's', ' ', 'n', 'o', ' ', 'm', 'e', 'n', 'u', ':', ' ', 'Y', 'o', 'u', ' ', 'g', 'e', 't', ' ', 'w', 'h', 'a', 't', ' ', 'y', 'o', 'u', ' ', 'd', 'e', 's', 'e', 'r', 'v', 'e']
5
6// explode by space
7str.split(' ') // ['Hear', 'about', 'the', 'new', 'restaurant', 'called', 'Karma,', 'There’s', 'no', 'menu:', 'You', 'get', 'what', 'you', 'deserve']
8
9// explode by comma
10str.split(',') // ['Hear about the new restaurant called Karma', ' There’s no menu: You get what you deserve']

Today, I’m going to show you How do i explode string in javascript, as above mentioned here, I’m going to use split() method to explode string via desired seprator.

Let’s start today’s tutorial how do you explode string in javascript?

In this example, we will do

  1. Take example of split string by char
  2. Take example of split string by space
  3. Take example of split string by comma
1// Take example of split string by char
2let str1 = "infinitbility";
3str1 = str1.split('');
4console.log(str1); // ['i', 'n', 'f', 'i', 'n', 'i', 't', 'b', 'i', 'l', 'i', 't', 'y']
5
6// Take example of split string by space
7let str2 = "Infinitbility want to make differance";
8str2 = str2.split(' ');
9console.log(str2); //  ['Infinitbility', 'want', 'to', 'make', 'differance']
10
11// Take example of split string by comma
12let str3 = "infinitbility,aguidehub,sortoutcode";
13str3 = str3.split(',');
14console.log(str3); // ['infinitbility', 'aguidehub', 'sortoutcode']

In the above exampls, we have taken multiple string example and explode by diferant parameters, Let’s check the output.

JavaScript, explode string in javascript example
JavaScript, explode string in javascript example

I hope it’s help you, All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

how to create array of objects in react native?

how to create array of objects in react native?

An example of create array of objects in react native with step by step guide.

June 25th, 2022 · 1 min read
how to remove item from array in react native?

how to remove item from array in react native?

An example of remove item from array in react native with step by step guide.

June 25th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]