我已經查找了如何大寫字符串的每個單詞的第一個字符,但沒有任何幫助。我需要將輸入的字符串設置爲標題大寫字母小寫字母。 我已經試過這樣:如何在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");
你是指每個字符串的第一個字符? – gurvinder372
請在這裏找到答案[在JavaScript中首字母大寫](http://stackoverflow.com/questions/1026069/capitalize-the-first-letter-of-string-in-javascript) –
是'I'小茶壺'預計輸出 – Tushar