如何在 JavaScript 中将两个字符串连接在一起,其中第一个字符串中有空格?
要连接两个字符串,我们需要使用 “+” 运算符在字符串之间留出一些空格,但是当第一个字符串本身包含空格时,就不必显式分配空格。
在下面的示例中,由于字符串“str1”本身包含空格,所以只需连接 而不留空格就足以连接这两个字符串。
示例
<html>
<body>
<script>
function str(str1, str2) {
return (str1 + str2);
}
document.write(str("tutorix is the best ","e-learning platform"));
</script>
</body>
</html>输出
tutorix is the best e-learning platform
如果第一个字符串中不存在空格,那么我们必须创建空格(“ ”)并连接这两个字符串,如下所示。
示例
<html>
<body>
<script>
function str(str1, str2) {
return (str1 + " " + str2);
}
document.write(str("tutorix is the best","e-learning platform"));
</script>
</body>
</html>输出
tutorix is the best e-learning platform
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP