最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

convert array to string in javascript - Stack Overflow

programmeradmin6浏览0评论

I want the text to be printed without commas.

<html>
<head>
<title>Reverse</title>
</head>
<body>
<form name="rev">
Enter the string : <input type="text" name="str"/>
<input type="button" value="click" onclick="rev1()" /><br>
reverse of given string : <input type="text" name="res"/>
</form>
<script type="text/JavaScript">
function rev1(){
var a=rev.str.value;
var b=[];
var i,j=0;
    for(i=a.length-1;i>=0;i--){
    b[j]=a[i];
    j++
}
//rev.res.value=b;
alert(b);
}
</script>
</body>
</html>

If I give the input as abc I am getting an output as c,b,a, but I want it as cba.

I want the text to be printed without commas.

<html>
<head>
<title>Reverse</title>
</head>
<body>
<form name="rev">
Enter the string : <input type="text" name="str"/>
<input type="button" value="click" onclick="rev1()" /><br>
reverse of given string : <input type="text" name="res"/>
</form>
<script type="text/JavaScript">
function rev1(){
var a=rev.str.value;
var b=[];
var i,j=0;
    for(i=a.length-1;i>=0;i--){
    b[j]=a[i];
    j++
}
//rev.res.value=b;
alert(b);
}
</script>
</body>
</html>

If I give the input as abc I am getting an output as c,b,a, but I want it as cba.

Share Improve this question edited Jul 29, 2012 at 12:03 Alex 9,4711 gold badge41 silver badges45 bronze badges asked Jul 29, 2012 at 12:01 user1560820user1560820 652 gold badges2 silver badges6 bronze badges 1
  • [].toString() is implemented as [].join() (which uses a comma as a separator, by default, ie [].join(','). – Rob W Commented Jul 29, 2012 at 12:07
Add a comment  | 

3 Answers 3

Reset to default 13

Try this:

alert( b.join("") )

You could also reverse a string more easily by:

"hello".split("").reverse().join("")
//"olleh"
  1. You may reverse your string using javascript built-in functions:

            <html>
            <head>
            <title>Reverse</title>
            </head>
            <body>
            <form name="rev">
            Enter the string : <input type="text" name="str"/>
            <input type="button" value="click" onclick="rev1()" /><br>
            reverse of given string : <input type="text" name="res"/>
            </form>
            <script type="text/JavaScript">
                function rev1(){
                    var a=rev.str.value;
                    var b=a.split("").reverse().join("");
                    //rev.res.value=b;
                    alert(b);
                }
            </script>
            </body>
            </html>
    
  2. You may also transfer join your array elements to become a string

            <html>
            <head>
            <title>Reverse</title>
            </head>
            <body>
            <form name="rev">
            Enter the string : <input type="text" name="str"/>
            <input type="button" value="click" onclick="rev1()" /><br>
            reverse of given string : <input type="text" name="res"/>
            </form>
            <script type="text/JavaScript">
                function rev1(){
                    var a=rev.str.value;
                    var b=[];
                    var i,j=0;
                        for(i=a.length-1;i>=0;i--){
                        b[j]=a[i];
                        j++
                    }
                    //rev.res.value=b;
                    alert(b.join(""));
                }
            </script>
            </body>
            </html>
    

You can also use document.getElementById().value.reverse().join("");

发布评论

评论列表(0)

  1. 暂无评论