the murmurous sea

JS, ES1: split() 본문

#dev/개념정리

JS, ES1: split()

lunacer 2020. 5. 4. 12:51

1. divides a String into an ordered set of substrings.
2. puts these substrings into an array, and returns the new array.  
3. The division is done by searching for a pattern; where the pattern is provided as the first parameter in the method's call.  

4. The split() method does not change the original string.

 

[Syntax]

str.split([separator[, limit]])
separator optional Specifies the character, or the regular expression, to use for splitting the string. 
The pattern describing where each split should occur. 
The separator can be a simple string or it can be a regular expression.
If separator contains multiple characters, that entire character sequence must be found in order to split.
If separator is omitted or does not occur in str, the entire string will be returned (an array with only one item)
If separator appears at the beginning (or end) of the string, it still has the effect of splitting.  
The result is an empty (i.e. zero length) string, which appears at the first (or last) position of the returned array.
If separator is an empty string (""), str is converted to an array of each of its UTF-16 "characters".
The str is not split by user-perceived characters (grapheme clusters) or unicode characters (codepoints), but by UTF-16 codeunits.
This destroys surrogate pairs.
limit optional
 
 
 
 
A non-negative integer.
specifies the number of splits.
splits the string at each occurrence of the specified separator, but stops when limit entries have been placed in the array. 
items after the split limit will not be included in the array
Any leftover text is not included in the array at all.
if the end of the string is reached before the limit is reached, The array may contain fewer entries than limit
If limit is 1, [str] is returned
If limit is 0 , [] is returned

 

Using split()
When the string is empty,   split() returns an array containing one empty string, rather than an empty array.
If the string and separator are both empty strings,  an empty array is returned.

 

[Return Value]

: An Array of strings, containing the splitted values.
: Split at each point where the separator occurs in the given string.
: The split() method does not change the original string.

 

[Example]

const myString = 'Hello World. How are you doing?'
const splits = myString.split(' ', 3)

console.log(splits)

//This script displays the following:
//["Hello", "World.", "How"]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split

https://www.w3schools.com/jsref/jsref_split.asp

 

 

 

 

 

 

 

'#dev > 개념정리' 카테고리의 다른 글

JS ES1: slice()  (0) 2020.05.04
JS, ES1: splice()  (0) 2020.05.04
JS: padStart() & padEnd()  (0) 2020.05.04
[HTML DOM] DocumentFragment  (0) 2020.05.02
HTML DOM: insertBefore()  (0) 2020.05.02
Comments