Phương thức string.concat[]
có chức năng nối hai hay nhiều chuỗi lại với nhau thành một chuỗi.
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.freetuts.net, không được copy dưới mọi hình thức.
Phương thức string.concat[]
sẽ không làm thay đổi các chuỗi truyền vào và sẽ trả về một chuỗi mới là kết quả của việc nối các chuỗi con truyền vào.
Cú pháp: string.concat[string1, string2, ..., stringX]
: string.concat[string1, string2, ..., stringX]
Trong đó:
Bài viết này được đăng tại [free tuts .net]
- string1, string2, ..., stringX là các chuỗi con sẽ được nối với chuỗi string.
Cách sử dụng
Ví dụ: sử dụng phương thức string.concat[]
để nối các chuỗi.: sử dụng phương thức string.concat[]
để nối các chuỗi.
Học lập trình miễn phí tại freetuts.net
Run function myFunction[] { var str = 'Học lập trình '; var str1 = 'miễn phí tại '; var str2 = 'freetuts.net'; var content = str.concat[str1, str2]; document.getElementById["demo"].innerHTML = content; }
Kết quả
Học lập trình miễn phí tại freetuts.net
Tham khảo: w3schools.com
Phương thức concat[] dùng để nối hai [hoặc nhiều] chuỗi lại với nhau thành một chuỗi.concat[] dùng để nối hai [hoặc nhiều] chuỗi lại với nhau thành một chuỗi.
Cú pháp:
string1.concat[string2, string3, ..., stringN]
Giá trị trả về của phương thức concat[] sẽ là một chuỗi được nối bởi các chuỗi: string1, string2, string 3, ...., stringN Lưu ý: Phương thức concat[] không làm thay đổi giá trị của các chuỗi ban đầu, nó chỉ nối các chuỗi ban đầu lại với nhau để tạo ra một chuỗi mới. |
var a = "Tài liệu học";
var b = "HTML";
var c = " CSS";
var chuoi1 = a.concat[b]; //Tài liệu họcHTML
var chuoi2 = a.concat[c]; //Tài liệu học CSS
var chuoi3 = a.concat[" ",b," và",c]; //Tài liệu học HTML và CSS
document.getElementById["demo1"].innerHTML = chuoi1;
document.getElementById["demo2"].innerHTML = chuoi2;
document.getElementById["demo3"].innerHTML = chuoi3;
Xem ví dụ
The
Học lập trình miễn phí tại freetuts.net0 method concatenates the string arguments to the calling string and returns a new string.
Học lập trình miễn phí tại freetuts.net0 method concatenates the string arguments to the calling string and returns a new string.
Try it
Syntax
concat[str1]
concat[str1, str2]
concat[str1, str2, /* …, */ strN]
Parameters
Học lập trình miễn phí tại freetuts.net1
One or more strings to concatenate to
Học lập trình miễn phí tại freetuts.net2.
Return value
A new string containing the combined text of the strings provided.
Description
The
Học lập trình miễn phí tại freetuts.net0 function concatenates the string arguments to the calling string and returns a new string. Changes to the original string or the returned string don't affect the other.
If the arguments are not of the type string, they are converted to string values before concatenating.
The
Học lập trình miễn phí tại freetuts.net0 method is very similar to the addition/string concatenation operators [
Học lập trình miễn phí tại freetuts.net5,
Học lập trình miễn phí tại freetuts.net6], except that
Học lập trình miễn phí tại freetuts.net0 coerces its arguments directly to strings, while addition coerces its operands to primitives first. For more information, see the reference page for the
Học lập trình miễn phí tại freetuts.net5 operator.
Examples
Using concat[]
The following example combines strings into a new string.
const hello = 'Hello, ';
console.log[hello.concat['Kevin', '. Have a nice day.']];
// Hello, Kevin. Have a nice day.
const greetList = ['Hello', ' ', 'Venkat', '!'];
"".concat[...greetList] // "Hello Venkat!"
"".concat[{}] // [object Object]
"".concat[[]] // ""
"".concat[null] // "null"
"".concat[true] // "true"
"".concat[4, 5] // "45"
Specifications
ECMAScript Language Specification # sec-string.prototype.concat # sec-string.prototype.concat |
Browser compatibility
BCD tables only load in the browser