Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 09-12-2009
Member
 
Join Date: Sep 2009
Posts: 143
JavaScript to convert an Array to String

I have an assignment program to be solved in JavaScript programming language. The program states to accept array values and then covert them to a string. I have tried several logic but all have failed. I believe there must be some inbuilt functions of JavaScript to enable this conversion. So can any one tell me how to go about this program to get the desired output using JavaScript.
Reply With Quote
  #2  
Old 09-12-2009
Member
 
Join Date: May 2008
Posts: 2,383
Re: JavaScript to convert an Array to String

You can use the toString() function to convert the values in an array to a string value in JScript. Here is an example:
Quote:
function ArrtoStr(Arr)
{
return Stng = Arr.toString();
}
This will accept an array and then convert it to string using the toString() and return it to the called section. Implement this code in your program.
Reply With Quote
  #3  
Old 09-12-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: JavaScript to convert an Array to String

Code:
<html>
<head>
<title>JavaScript Conversion using toString</title>
<script>
var ray = new Array();

function push(val)
{          
if(val=="")
alert("Required Field")

else
{
ray[ray.length]=val;
var string = "<hr><b>List</b>";
for(int j = 0; j < ray.length; j++)
{
string = string + "<br>" + ray[j];
              }
string = string +"<br><br>Array 
to String conversion<br>"+
ray.join(" ");

document.frmA.string.value = ""
document.getElementById('myDiv').innerHTML = string;
}
}
</script>
</head>
<body>
<B>Conversion of Array</B>
<form name="frmA">
<table>
<tr>
<td align="right">String</td>
<td>&nbsp;</td>
<td>
<input type="text" name="string"/></td>
</tr>
<tr>
<td align="right">&nbsp;</td>
<td >&nbsp;</td>
<td width="224">
<input type="button" Value=" Insert " onclick="push(this.form.string.value)"/>
 </td>
</tr>
</table>
</form>
<div id="Div1"></div>
</body>
</html>
This is JavaScript program that I used to accept values in Array from the user which is then displayed as String in one single line. Also the validation is done so that the user does not enters a Null value or leaves it blank.
Reply With Quote
  #4  
Old 09-12-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
Re: JavaScript to convert an Array to String

I also had a similar program for my exam and I did the following coding for it:
Code:
<html>
<body>
<script type="text/javascript">
var Temp = ["Ross", "Mark", "McDonald"];
document.write(Temp.toString());
</script>
</body>
</html>
As the output it is displayed as:
Quote:
Ross,Mark,McDonald
The toString() function forms the bases for the array to be converted to a string in JavaScript
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "JavaScript to convert an Array to String"
Thread Thread Starter Forum Replies Last Post
String is not occurring in array. rUChIRr Software Development 6 17-06-2011 10:20 PM
Splitting string into array with delimiters Paul$Terry Software Development 6 10-06-2011 07:09 AM
How to convert string to char array in java? Baazigar Software Development 6 10-01-2011 06:36 PM
NullPointerException when using array of string in Java Sean J Software Development 2 25-04-2009 08:14 PM
PHP: array to string? Diwakar_12 Software Development 4 13-04-2009 02:44 PM


All times are GMT +5.5. The time now is 06:26 AM.