Javascript 将字符数组转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7425374/
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-08-24 02:13:15 来源:igfitidea点击:
Convert a Char Array to a String
提问by moey
How do you convert an array of characters to a string in JavaScript?
在 JavaScript 中如何将字符数组转换为字符串?
var s = ['H', 'e', 'l', 'l', 'o'];
// How to convert s to a string?
回答by Gabriel Ross
You do it this way:
你这样做:
var str = s.join();
回答by kennebec
Or use String.
或者使用字符串。
var string = String([1,2,3]);
回答by Flavio Sousa
The join command lets you set the token among the items in the array.
join 命令允许您在数组中的项目之间设置标记。
Ex1:
例1:
function print(str) {
$("#result").append("<p>" + str + "</p>");
}
print(["A", "B", "C"].join()); // "A,B,C"
print(["A", "B", "C"].join("-")); // "A-B-C"
print(["A", "B", "C"].join("||")); // "A||B||C"
print(["A", "B", "C"].join("")); // "ABC"
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="result"></div>
回答by Alessandro Annini
this is the easiest and fastest way;
这是最简单快捷的方式;
s = s + '';