Home > Web Front-end > JS Tutorial > Basic methods of string concatenation and segmentation

Basic methods of string concatenation and segmentation

巴扎黑
Release: 2016-11-25 14:22:29
Original
2777 people have browsed it

The following are the basic methods of string splicing and segmentation, for reference only:

1. String splicing

The most basic method is to use '+' to "add" two strings, for example:

Java code

var name='Nicholas'+' '+'Conti';

console.log(name);//The result is: Nicolas Conti

If the string is too long or you want to splice it step by step, you can choose "+=" operator:

Java code

var str='Uncle'+'is'+'benevolent'+',';

str +='Love the people like a child '+', '+'The specialty is the ability to cry';

console.log(str)//The result is: The emperor's uncle is benevolent, loves the people like a son, and the specialty is the ability to cry

There is another The method is to use the concat() method:

Java code

var string='Yun Chang has a red face,';

string.concat('The beard is quite long, '+' is sure to be conceited ,'+'Unlucky will come sooner or later')//Result: Yun Chang is red-faced, conceited, unlucky will come sooner or later

In addition, the concat() method can also be used to splice arrays:

Java code

var array=['老','夫'];

var array2=['Fa','Da','Le'];

console.log(array.concat(array2)); //The result is: ['Lao','Fu','Fa','Da','Le']

Moreover, the concat() method will not change the original array, and the concatenation will form a new array;

It should also be noted that after concatenating strings, they are still strings, and after concatenating arrays, they are still arrays.

2. String splitting

The split() method has various usages:

We can split it in several different ways:

Java code

var string= 'I ate ten bowls today Face'

console.log(string.split(''));//Result ['I', ' ', 'Jin', ' ', 'Tian', ' ', 'eat', ' ', ' ', ' ', '十', ' ', 'bowl', ' ', 'noodles']

console.log(string.split(' '));//Result ['I', 'Jin' , 'day', 'eat', 'le', 'ten', 'bowl', 'noodles']

console.log(string.split());//Result ['I ate ten bowls today Face']

We can see that using an empty string will separate each character, using spaces to split the string by spaces, and not using parameters will divide the entire string into an array with only one string element. Note that no matter which splitting method is used, the result is an array.

Above we only used the first parameter of the split() method. When using it, you can change the first parameter in the brackets as needed to select the split position. In fact, the split() method also has a second parameter. , use the second parameter to specify the length of the array after splitting:

Java code

var string= 'I ate a bowl of noodles today'

console.log(string.split(' ',6) ); Result: ['I', 'Jin', 'Tian', 'Eat', 'Le', 'Bowl']

console.log(string);//I ate a bowl of noodles today

In addition, as you can see from the last line, the metastring will not be changed after splitting using the split() method.


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template