1

我将字符串拆分为“”,并将所有单词存储在strSplit中。使用FORLOOP将每个单词的第一个字母大写并存储在变量firstLetter中。将单词的其余部分存储在变量restLetter中。然后使用+添加firstLetter和restLetter,并将结果存储在newLetter中。现在我想使用join去掉每个单词中的“”,使其成为一个字符串,每个单词中每个首字母都大写。但如果我在newLetter上申请加入,它就不起作用了。

函数titleCase(str){var strSplit=(str.split(“”));var newLetter=[];var returnString;var firstLetter;var restLetter;对于(i=0;i<=(strSplit.length-1);i++){firstLetter=strSplit[i].charAt(0).toUpperCase();对于(i=0;i<=(strSplit.length-1);i++){firstLetter=strSplit[i].charAt(0).toUpperCase();restLetter=strSplit[i].slice(1,str.Split);newLetter=firstLetter+restLetter;newLetter.join(“and”);}  return newLetter;}}titleCase(“我是一个小茶壶”);
2
  • 我找到了解决方案!!这里是:-function titleCase(str){var strSplit=(str.split(''));var newLetter=[];var returnString;var firstLetter;var restLetter,for(i=0;i<strSplit.length;i++){firstLeter=strSplit[i].charAt(0).toUpperCase(); newLetter.push(firstLetter+restLetter)//newLetter.join(“”);}return newLetter.join(“”)//return newLetter;}//titleCase(“我是一个小茶壶”); 评论 2018年1月22日2:26
  • 函数titleCase(str){var strSplit=(str.split(''));var newLetter=[];var returnString;var firstLetter;var restLetter,用于(i=0;i<strSplit.length;i++){firstLeter=strSplit[i].charAt(0).toUpperCase();//console.log(firstLeter);restLeter=strSplit[i].slice(1,str.split).toLowerCase(;newLetter.push(firstLetter+restLetter)//newLetter.join(“”);}return newLetter.join(“”)//return newLetter;}//titleCase(“我是一个小茶壶”); 评论 2018年1月22日2:27

2个答案2

重置为默认值
2

你可以很容易地这样做:

函数titleCase(str){返回str.split(“”).map(word=>word.charAt(0).toUpperCase()+word.slice(1).toLowerCase(.join(“”)}log(titleCase(“我是一个小茶壶”);

在这里,我们分裂通过空格,然后使用地图然后将第一个字符连接为大写,将单词的其余部分连接为小写,然后参加单词用空格排列。

0
0

我用我开始的相同方法自己找到了解决方案!!这里是:

函数titleCase(str){var strSplit=(str.split(“”));var newLetter=[];var firstLetter;var restLetter;对于(i=0;i<strSplit.length;i++){firstLetter=strSplit[i].charAt(0).toUpperCase();restLetter=strSplit[i].slice(1,str.Split).toLowerCase();newLetter.push(firstLetter+restLetter);}  return newLetter.join(“”);}titleCase(“我是一个小茶壶”);

你的答案

单击“发布您的答案”,表示您同意我们的服务条款并确认您已阅读我们的隐私政策.

不是你想要的答案吗?浏览标记的其他问题问你自己的问题.