Javascript 在字符串中的字符之间添加一个空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7437385/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:16:32  来源:igfitidea点击:

Add a space between characters in a String

javascriptstring

提问by freshest

Possible Duplicate:
String Manipulation - Javascript -

可能的重复:
字符串操作 - Javascript -

I have a string:

我有一个字符串:

hello

and I want to add a space between each character to give:

我想在每个字符之间添加一个空格以给出:

h e l l o

What is the best way to do this?

做这个的最好方式是什么?

回答by davin

"hello".split('').join(' '); // "h e l l o"

回答by Mike Edwards

var text = "hello";
var betweenChars = ' '; // a space

alert(text.split('').join(betweenChars));

回答by ericb

try:

尝试:

var hello = 'hello';
var test = '';

for(var i=0; i<hello.length; i++){
   test += hello.charAt(i) + ' ';     
}

alert(test);