input=“你好,世界,怎么了?”
-
你真的想要“whats up”还是“What up”? – 克里斯蒂安·佐塞尔 2016年10月22日18:48 -
请阅读此 stackoverflow.com/questions/1026069/… – 伊凡娜·赫尔文 2016年10月22日18:49 -
1 input.charAt(0).toUpperCase()+input.toLowerCase(().substring(1,txt.length); – 斯马 2016年10月22日18:50 -
@SMA将除第一个单词的第一个字母外的所有字母小写。 如果第一个字母已经是大写的,我基本上不想不小写 – 贾森 2016年10月22日18:54 -
这回答了你的问题吗? 如何在JavaScript中使字符串的第一个字母大写? – 康纳·洛 2022年8月15日15:06
4个答案
const upperCaseFirstLetter=string=> `${string.slice(0,1).toUpperCase()}${string切片(1)}`; const lowerCaseAllWordsExceptFirstLetters=字符串=> string.replaceAll(/\S*/g,单词=> `${word.slice(0,1)}${work.slice(1).toLowerCase()}` ); const input='hello World,whatS up?'; const desiredOutput=upperCaseFirstLetter(lowerCaseAllWordsExceptFirstLitters(input)); console.log(desiredOutput);
函数titleCase(str){ return str.split('').map(项=> item.charAt(0).toUpperCase()+item.slice(1).toLowerCase(()).join(“”); } console.log(titleCase(“这是我的手柄,这是我自己的喷口”); //它会打印“这是我的把手,这是我喷口”;