我希望打印的文本不带逗号。
<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>
如果我将输入作为 abc
,我将得到作为 c,b,a
的输出,但我希望它作为 cba
。
最佳答案
试试这个:
alert( b.join("") )
您还可以通过以下方式更轻松地反转字符串:
"hello".split("").reverse().join("")
//"olleh"
关于javascript - 在javascript中将数组转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11709086/