小编Mam*_*hla的帖子

如何在javascript中字符串的每个单词的首字母大写?

我一直在寻找如何大写字符串中每个单词的第一个字符,但没有任何帮助。我需要将输入的字符串设置为大写小写的标题。我已经试过了:

function titleCase(str) {
//converting the giving string into array
  str =str.split(" "); 
//iterating over all elem.s in the array
  for(var i=0;i<str.length;i++){        
//converting each elem. into string
    str[i]=str[i].toString(); 
//converting the first char to upper case &concatenating to the rest chars
    str[i]=str[i].toUpperCase(str[i].charAt(0))+ str[i].substring(1);
  }
  return str;
}
titleCase("I'm a little tea pot");
Run Code Online (Sandbox Code Playgroud)

javascript

7
推荐指数
1
解决办法
581
查看次数

标签 统计

javascript ×1