split()
MethodYou can use the JavaScript split()
method to split a string using a specific separator such as comma (,
), space, etc. If separator is an empty string, the string is converted to an array of characters.
The following example demonstrates how to convert a comma separated string of person name into an array and retrieve the individual name using JavaScript.
<script>
var names = 'Harry,John,Clark,Peter,Rohn,Alice';
var nameArr = names.split(',');
console.log(nameArr);
// Accessing individual values
alert(nameArr[0]); // Outputs: Harry
alert(nameArr[1]); // Outputs: John
alert(nameArr[nameArr.length - 1]); // Outputs: Alice
var str = 'Hello World!';
var chars = str.split('');
console.log();
// Accessing individual values
alert(chars[0]); // Outputs: H
alert(chars[1]); // Outputs: e
alert(chars[chars.length - 1]); // Outputs: !
</script>
Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on harsukh21@gmail.com
How to Check for a Hash (#) in a URL using JavaScript
Use the Window.location Property You...How to create custom checkboxes in HTML using CSS and jQuery
Use the CSS :checked Pseudo-class with j...How to integrate Rozorpay Payment Gateway in Laravel 8
Today, I will share with you how to inte...How to show and hide div elements based on the selection of radio buttons in jQuery
Use the jQuery show() and hide() methods...How to disable or enable a form element using jQuery
Use the jQuery prop() method...