Add a space between characters in a String [duplicate]
Asked Answered
M

3

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?

Myrna answered 15/9, 2011 at 20:59 Comment(1)
This may be a duplicate but it has a better answer than the other question, I think we should re-open thisMichellmichella
F
103
"hello".split('').join(' '); // "h e l l o"
Firebox answered 15/9, 2011 at 21:2 Comment(0)
V
4
var text = "hello";
var betweenChars = ' '; // a space

alert(text.split('').join(betweenChars));
Verde answered 15/9, 2011 at 21:2 Comment(0)
B
4

try:

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

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

alert(test);
Bendicty answered 15/9, 2011 at 21:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.