简体   繁体   English

Javascript 中字符串和变量之间的空格

[英]Space between string & variable in Javascript

How can I make this return "Hello yourName" as opposed to "HelloyourName"?我怎样才能让这个返回“Hello yourName”而不是“HelloyourName”?

    var yourName=(prompt("What's your name?"));
    alert("Hello" + yourName);       
    
alert("Hello " + yourName.trim());    
alert("Hello" + " " + yourName.trim());    

I don't see why you cannot add a space between.我不明白为什么你不能在它们之间添加空格。 yourName.trim() is to make sure you won't introduce unexpected spaces by yourname . yourName.trim()是为了确保您不会通过yourname引入意外的空格。

Add a space character to the string.在字符串中添加一个空格字符。

This:这个:

"Hello" + yourName

would resolve to this:会解决这个问题:

HelloJesse

whereas this:而这:

"Hello " + yourName
   // ^--- added a space

would resolve to this:会解决这个问题:

Hello Jesse
var yourName=(prompt("What's your name?"));
alert("Hello " + yourName); // "Hello " with space  

You can concatenate a space in between the variables, like so:您可以在变量之间连接一个空格,如下所示:

var fullName = firstName + ' ' + lastName; var fullName = firstName + ' ' + lastName;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从字符串变量 cardLabel 中删除除 {Group} 和 {Desc} 之外的所有内容,并在 javascript 中在它们之间添加一个空格? - how to remove everything except {Group} and {Desc} from string variable cardLabel and add a space between them in javascript? Javascript:检查字符串定界符之间是否有空格 - Javascript: Check if there is any space between a string delimiter 使用 javascript 删除字符串之间的空格 - Remove white space between the string using javascript JavaScript中字符串中小写和大写字母之间的空格 - Space between lowercase and uppercase letters in a string in JavaScript Javascript字符串/无空间的变量串联 - Javascript string/variable concatenation without space Javascript中带空格的“未定义”变量的字符串串联 - String concatenation for “undefined” variable with space in Javascript 我的代码在字符串和变量之间添加空格? 为什么这样做呢? - My code is adding a space between the string and the variable? Why is it doing that? 如何获取数组中的所有内容并将其分配给一个变量,中间有一个空格作为字符串 - How to get everything within an array and assign it to a variable with a space in between as a string 如何在JavaScript中使用正则表达式在数字和字符/字符串之间插入空格 - How to insert space between a number and a character /string using regex in javascript 在空格前获取一些字符串,然后使用Javascript将其保存在变量中 - Get some string before the space, and save it in a variable with Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM