Results 1 to 4 of 4

Thread: JavaScript to convert an Array to String

  1. #1
    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.

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    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:
    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.

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    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.

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    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:
    Ross,Mark,McDonald
    The toString() function forms the bases for the array to be converted to a string in JavaScript

Similar Threads

  1. String is not occurring in array.
    By rUChIRr in forum Software Development
    Replies: 6
    Last Post: 17-06-2011, 10:20 PM
  2. Splitting string into array with delimiters
    By Paul$Terry in forum Software Development
    Replies: 6
    Last Post: 10-06-2011, 07:09 AM
  3. How to convert string to char array in java?
    By Baazigar in forum Software Development
    Replies: 6
    Last Post: 10-01-2011, 06:36 PM
  4. NullPointerException when using array of string in Java
    By Sean J in forum Software Development
    Replies: 2
    Last Post: 25-04-2009, 08:14 PM
  5. PHP: array to string?
    By Diwakar_12 in forum Software Development
    Replies: 4
    Last Post: 13-04-2009, 02:44 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,943,888.43950 seconds with 17 queries