Article directory
js method of splitting strings
Applicable scenarios are different
const arr=['1-2','1-3','2-3','3-4'];
1、string.split()
Split with special characters
'1-2'.split('-')
// ['1', '2']
2、String.substring(start,stop)
start is required. A non-negative integer specifying the position in string of the first character of the substring to be extracted.
stop is optional, a non-negative integer, the header does not include the tail, and it needs to add 1 to the last target character number.
'hello'.substring(1,3)
// 'el'
3. Use String.substr(start, lenght) to split the string
start is required. The starting subscript of the substring to be extracted. Must be a numeric value. If negative, the parameter specifies the position from the end of the string. That is, -1 refers to the last character in the string, -2 refers to the second-to-last character, and so on.
length is optional. The number of characters in the substring. Must be a numeric value. If this parameter is omitted, the string from the beginning to the end of string is returned.
'hello'.substr(1,3)
// 'ell'
4. Use String.slice(start, end) to split the string
Extracts a portion of a string and returns the extracted portion as a new string.
'hello'.slice(1,3)
// 'el'