var str="this is my new tutorial";
var a1 = new Array();
a1=str.split(" ");
document.write(a1.join(" <br> "));
We have used join to generate a string by joining elements through line breaks. Here is the output
this
is
my
new
tutorial
We have seen how to create string using elements of an array. Now we will see how to create an array from a string by using delimiter and split().
var array_name=string_variable.split(" ");
var str="this is my new tutorial";
var a1 = new Array();
a1=str.split(" ");
document.write(a1.join(" <br> "));
We have used join to generate a string by joining elements through line breaks. Here is the output
this
is
my
new
tutorial
<script type="text/javascript">
var str="Welcome to plus2net";
var a1 = new Array();
a1=str.split("");
/// display elements ///
for (i=0;i<a1.length;i++)
{
document.write(a1[i] + "<br >");
}
</script>
Output is here
W
e
l
c
o
m
e
t
o
p
l
u
s
2
n
e
t
<script type="text/javascript">
var str="Welcome to plus2net. You can learn web programming and use them in your applications";
var a1 = new Array();
a1=str.split(" ",3);
/// display elements ///
for (i=0;i<a1.length;i++)
{
document.write(a1[i] + "<br >");
}
</script>
The output is here
Welcome
to
plus2net.
<script type="text/javascript">
var str="username@example.com";
var a1 = new Array();
a1=str.split("@");
document.write (a1[0] + "<br>" + a1[1]);
</script>
Output is here
username
example.com
Demo of creating array from a string by using split
max | 24-10-2012 |
Using a for loop (not a for-in loop.) Output 0 to 9 comma delineated no spaces. Output will be 0,1,2,3,4,5,6,7,8,9, in javascript |