javascript 在javascript中将数组转换为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11709086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:05:24  来源:igfitidea点击:

convert array to string in javascript

javascript

提问by user1560820

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 abcI am getting an output as c,b,a, but I want it as cba.

如果我abc在获得输出时给出输入c,b,a,但我希望它作为cba.

回答by Esailija

Try this:

试试这个:

alert( b.join("") )

You could also reverse a string more easily by:

您还可以通过以下方式更轻松地反转字符串:

"hello".split("").reverse().join("")
//"olleh"

回答by Michael

  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>
    
  1. 您可以使用 javascript 内置函数反转您的字符串:

            <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. 你也可以将你的数组元素调用join变成一个字符串

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

回答by abdul

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

你也可以使用 document.getElementById().value.reverse().join("");