开发者

Convert a Char Array to a String

How do you convert an array of characters to a string in JavaScript?

var s = ['开发者_JS百科H', 'e', 'l', 'l', 'o'];
// How to convert s to a string?


Use join:

string = s.join("");


You do it this way:

var str = s.join();


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>


Or use String.

var string = String([1,2,3]);


If you have an array like let array1 = ['a', 'b', 'c'] you can try array1.join('') the ourput will be 'abc'

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜