有人可以帮我如何将arr转换成字符串吗?

时间:2017-11-08 20:06:28

标签: javascript string

function titleCase(str) {

  var temp = "";
  var arr;
  var stri = str.toLowerCase();

  var words = stri.split(" ");
  for (var i = 0; i < words.length; i++)
    arr = arr + words[i].replace(words[i].charAt(0), function(temp) {
      return temp.toUpperCase();
    });
  return arr;

}

titleCase("I'm a little tea pot");

2 个答案:

答案 0 :(得分:0)

您可以使用arr.join(" ");

https://www.w3schools.com/jsref/jsref_join.asp

答案 1 :(得分:0)

您可以拆分字符串,将第一个字符作为大写字母并添加字符串的其余部分。最后,将数组连接到带空格的字符串。

&#13;
&#13;
function titleCase(string) {
    return string
        .split(' ')
        .map(s => s[0].toUpperCase() + s.slice(1))
        .join(' ');
}

console.log(titleCase("I'm a little tea pot"));
&#13;
&#13;
&#13;