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

Add a space between characters in a String

本文关键字:之间 添加 空格 字符 字符串      更新时间:2023-09-26

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

我有一个字符串:

hello

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

h e l l o

最好的方法是什么?

"hello".split('').join(' '); // "h e l l o"
var text = "hello";
var betweenChars = ' '; // a space
alert(text.split('').join(betweenChars));

try:

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