Split Text in javascript

Something Like that:
split()

For example I have a word “Home2it”. I want to should be split like H,o,m,e,2,i,t
Home2it

For that you need to create first html code.

HTML

<p id="demo"></p>

After that add this JS code.

JS

var str = "Home2it";
var arr = str.split("");
var res = "";
var i;
for (i = 0; i < arr.length; i++) {
    res += arr[i] + ","
}
document.getElementById("demo").innerHTML = res;

[codepen_embed height="265" theme_id="0" slug_hash="ayvLXd" default_tab="result" user="pradeepanvi"]See the Pen Split Text in javascript by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]

Now If you want to change "," into "|"

JS

var str = "Home2it";
var arr = str.split("");
var res = "";
var i;
for (i = 0; i < arr.length; i++) {
    res += arr[i] + "|"
}
document.getElementById("demo").innerHTML = res;

Or text Like this
H
o
m
e
2
i
t

JS

var str = "Home2it";
var arr = str.split("");
var res = "";
var i;
for (i = 0; i < arr.length; i++) {
    res += arr[i] + "
" } document.getElementById("demo").innerHTML = res;

Leave a Reply

Your email address will not be published. Required fields are marked *