JavaScript concat() 方法
连接两个字符串:
<p id="demo">单击按钮连接两个字符串成新的字符串。</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction() {
var txt1 = "Hello ";
var txt2 = "world!";
var n = txt1.concat(txt2);
document.getElementById("demo").innerHTML = n;
}
</script>
</body>
定义和用法
concat() 方法用于连接两个或多个字符串。
该方法没有改变原有字符串,但是会返回连接两个或多个字符串新字符串。
浏览器支持
![]()
![]()
![]()
![]()
![]()
所有主要浏览器都支持 concat() 方法
语法
string.concat(string1, string2, ..., stringX)
参数值
| 参数 | 描述 |
|---|---|
| string1, string2, ..., stringX | 必需。将被连接为一个字符串的一个或多个字符串对象。 |
返回值
| 类型 | 描述 |
|---|---|
| String | 两个或多个字符串连接后生成的新字符串。 |
技术细节
| JavaScript 版本: | 1.2 |
|---|
更多实例
连接3个字符串:
<p id="demo">单击按钮连接2个字符串形成一个新的字符串。</p>
<button onclick="myFunction()">点我</button>
<script>
function myFunction() {
var txt1 = "Hello ";
var txt2 = "world!";
var txt3 = " Have a nice day!";
var n = txt1.concat(txt2, txt3);
document.getElementById("demo").innerHTML = n;
}
</script>
JavaScript String 对象
JavaScript String 对象