JavaScript String concat() 方法将两个或多个字符串参数连接到当前字符串。此方法不会更改现有字符串的值,但会返回一个新字符串。
您可以根据需要连接多个字符串参数。
语法
以下是 JavaScript String concat() 方法的语法 -
参数
此方法接受多个字符串作为参数。与下面描述的相同 -
- str1, str2,....., strN − 要连接的字符串。
返回值
此方法在连接后返回一个新字符串。
示例 1
在下面的程序中,我们使用 JavaScript String concat() 方法将两个字符串 “qikepu” 和 “com” 连接起来。
输出
上面的程序在连接后返回一个新字符串 “qikepucom”。
str1 = qikepu
str2 = com
new_str(after concatenate) = qikepucom
str2 = com
new_str(after concatenate) = qikepucom
示例 2
以下是 String concat() 方法的另一个示例。在此示例中,我们将多个字符串连接起来:“Welcome”、“to”、“qikepu”和“com”,以及空格以分隔它们。
输出
执行上述程序后,它将连接所有字符串并返回一个新字符串作为 -
str1 = Welcome
str2 = to
str3 = qikepu
str4 = com
Concatenated string(with white-sapce separated) = Welcome to qikepu com
Concatenated string(without white-sapce separated) = Welcometoqikepucom
str2 = to
str3 = qikepu
str4 = com
Concatenated string(with white-sapce separated) = Welcome to qikepu com
Concatenated string(without white-sapce separated) = Welcometoqikepucom
示例 3
让我们看看如何使用 JavaScript String concat() 方法的另一个示例。使用这种方法,我们可以连接字符串 “Hello” 和 “World” 并用逗号 (,) 分隔它们。
输出
上面的程序在输出中返回 “Hello, World” -
Hello, World