作者:robable004_96978 | 来源:互联网 | 2022-01-26 03:10
非常简单,就用一个"+"将两个字符串"相加":
var lOngString= "One piece " + "plus one more piece.";
要将多个字符串累积为一个字符串,还可以使用"+="操作符:
var result = "";
result += "My name is Anders"
result += " and my age is 25";
要在字符串中添加换行符,需要使用转义字符"":
var cOnfirmString= "You did not enter a response to the last " +
"question.Submit form anyway?";
var cOnfirmValue= confirm(confirmString);
但这种方法只能用在像警告、确认对话框之类的情况下,如果将这段文本作为HTML内容呈现,就无效了,此时 用"
"代替它:
var htmlString = "First line of string.
Second line of string.";
document.write(htmlString);
String对象还提供了方法concat(),它完成与"+"相同的功能:
string.concat(value1, value2, ...)
不过concat()方法显然不如"+"来得直观简洁。