-
Notifications
You must be signed in to change notification settings - Fork 2
JS String Prototype Split
The split() method splits a string into an array of strings.
Syntax
str.split(separator[, limit]);
separator Optional. The separator specifies which characters to use for separating the string. If separator is omitted str.split();
the array will contain just one element consisting of the entire string. If separator is an empty string str.split("");
the string is converted into an array of characters.
limit Optional. Whole number integer that sets a limit to the number of splits that can be found. The split()
method will still find all of the matches to the separator, but will truncate the returned array to a maximum length of limit
elements.
The split() method returns the new array.
At each match of the separator .split()
will split the string and add the split section as an element to an array.
Splitting a string into an array of substrings
var str = "HelloWorld";
console.log(str.split(""));
// Console will output: ["H", "e", "l", "l", "o", "W", "o", "r", "l", "d"]
var str2 = "HelloWorld";
console.log(str2.split());
// Console will output: ["Hello World"]
var str3 = "HelloWorld";
console.log(str3.split("Wo"));
// Console will output: ["Hello", "rld"]
Source MDN
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links