如何在 JavaScript 中将字符数组转换为字符串?
var s = ['H', 'e', 'l', 'l', 'o']; // How to convert s to a string?
Use join:
join
string = s.join("");
You do it this way:
var str = s.join();
Or use String.
var string = String([1,2,3]);
The join command lets you set the token among the items in the array.
Ex1:
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>
If you have an array like let array1 = ['a', 'b', 'c'] you can try array1.join('') the ourput will be 'abc'
let array1 = ['a', 'b', 'c']
array1.join('')
'abc'