如何在 JavaScript 中添加两个字符串,第一个字符串中有空格?
front end technologyobject oriented programmingjavascript更新于 2024/5/17 10:55:00
要添加两个字符串,我们需要一个 '+' 运算符在字符串之间创建一些空格,但是当第一个字符串本身包含空格时,无需明确分配空格。
在下面的例子中,由于字符串 '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