开发者

Add a space between characters in a String [duplicate]

开发者 https://www.devze.com 2023-04-05 09:20 出处:网络
This question already has answers here:开发者_运维百科 Closed 11 years ago. Possible Duplicate: String Manipulation - Javascript -
This question already has answers here:开发者_运维百科 Closed 11 years ago.

Possible Duplicate:

String Manipulation - 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?


"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);
0

精彩评论

暂无评论...
验证码 换一张
取 消