您的位置:首页 > 其它

Title Case a Sentence

2017-03-03 12:33 155 查看


Return the provided string with the first letter of each word capitalized. Make sure the rest of the word is in lower case.

For the purpose of this exercise, you should also capitalize connecting words like "the" and "of".

思路:1.将字符串全部转换为小写,然后转为数组var array=str.toLowerCase().split(" ");

2.遍历数组,将数组中字符的第一个字符转换为大写toUpperCase(),然后[b]substring(1,array[i].length),截取字符串中的其余位。[/b]

[b][b]3.将数组转换为字符串,并返回[/b][/b]

function titleCase(str) {

  var array=str.toLowerCase().split(" ");

  for(var i=0;i<array.length;i++){

    

    array[i]=array[i][0].toUpperCase()+array[i].substring(1,array[i].length);

  }

  var string=array.join(" ");

  return string;

}

titleCase("I'm a little tea pot");
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: