javascript split string – How do I split a string, breaking at a particular character?

javascript split string – The split() method splits a string into an array of substrings, and returns the new array. Split a string with multiple separators. Once split it returns an array containing a substring.

string.split(separator, limit)

javascript split string – JavaScript String split() Method

Use the JavaScript String split() to divide a string into an array of substrings by a separator. ·

JavaScript split string code:

let Flexiple = 'Main top doctors developers'
let nurces = 'Pavan,Bhoomi,Sheetal,krishna,Rekha,Kavya,Manisha'

let flexiplelist = Flexiple.split(" ")
let nurceslist = nurces.split(",")
let flexiplelist2 = Flexiple.split()
let nurceslist2 = nurces.split(",",3)

console.log(nurceslist)
console.log(flexiplelist)
console.log(flexiplelist2)
console.log(nurceslist2)

The result for the above code is:

> Array ["Pavan", "Bhoomi", "Sheetal", "krishna", "Rekha", "Kavya", "Manisha"]
> Array ["Main", "top", "doctors", "developers"]
> Array ["Main top doctors developers"]
> Array ["Pavan", "Bhoomi", "Sheetal"]

How to Split a String into an Array in JS?

let userInput = "Yes, You Can Do It!";

console.log(userInput[0]); // Y
console.log(userInput[1]); // e
console.log(userInput[2]); // s
console.log(userInput[3]); // ,

console.log(userInput[10]); // a

The split() Method in JavaScript

let message = 'I am a Hartless Go successful Member';

Split using a space character

let arr = message.split(' ');

// The array
console.log(arr); // ["I", "am", "a", "Hartless", "Go", "successful", "Member"]


// Access each of the elements of the array.
console.log(arr[0]); // "I"
console.log(arr[1]); // "am"
console.log(arr[2]); // "a"
console.log(arr[3]); // "Hartless"
console.log(arr[4]); // "Go",
console.log(arr[5]); // "successful"
console.log(arr[6]); // "Member"

Don’t Miss : Javascript Split String By Comma

Also Read This 👉   Remove readonly attribute jquery

How to Split a String into One Array?

let message = 'I am a Hartless Go successful Member';
console.log(message.split()); // returns ["I am a Hartless Go successful Member"]

examples

// Returns an empty array
''.split(''); // returns []

// Returns an array with an empty string
''.split() // returns [""]

How to Split with a Limit?

string.split(splitter, limit);

let message = 'I am a Hartless Go successful Member';
console.log(message.split(' ', 4)); // ["I", "am", "a", "Hartless"] 

How to Split Using Regex

let message = 'How to Integrate Stripe Payment Gateway in Codeigniter';

//OR

let final_conct = message.split(/[.,!,?]/);
console.log(final_conct);

How to Replace Characters in a String using Split() Method?

let name = 'Bhoomi Krishna';
let result = name.split(' ');
console.log(result); // ["Bhoomi", "Krishna"]

let final_match = result.join('-');
console.log(final_match); // Bhoomi-Krishna

I hope you get an idea about javascript split string.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.