上周看到大家在JS群讨论如何把一句英文句子单词收割字母大写,
大家都说用正则简单,对于正则还是有点模糊,于是乎自己敲了下
//面试题:如何把一句英文每个单词首字母大写?
var str="what fuck is 1235 going on ?";
var arr=str.split(" ");
for(var i=0,newArr=[],sumArr=[];i<arr.length;i++){
var news=arr[i][0].toUpperCase();
newArr.push(news);
var sums=arr[i].slice(1);
sumArr.push(sums);
arr[i]=newArr[i]+sumArr[i];
}
str=arr.join(" ");
console.log(arr,str);//["What", "Fuck", "Is", "1235", "Going", "On", "?"] "What Fuck Is 1235 Going On ?"
结果是出来了,总感觉不是很好、、、