35

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?

1
  • This may be a duplicate but it has a better answer than the other question, I think we should re-open this
    – PW Kad
    Aug 20, 2014 at 12:45

3 Answers 3

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

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

try:

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

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

alert(test);

Not the answer you're looking for? Browse other questions tagged or ask your own question.