function capitalizeFirstLetterWithRegex(string) { return string.replace(/(^\w{1})/, s => s.toUpperCase()); } console.log(capitalizeFirstLetterWithRegex("js")); // 输出: "Js" 方法3:使用扩展运算符和 map javascript function capitalizeFirstLetterWithSpread(string) { return [...string]...
function capitalizeFirstLetter(str) { return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase(); } var str = "hello world"; var capitalizedStr = capitalizeFirstLetter(str); console.log(capitalizedStr); // 输出 "Hello world"
function capitalizeFirstLetter(str) { return str.charAt(0).toUpperCase() + str.slice(1); } 19. 判断数组是否包含特定元素 在处理数组时,你可能需要确定数组中是否包含特定元素。arrayContains函数接受一个数组和一个要查找的元素作为参数,然后返回一个布尔值,指示数组是否包含该元素。 function arrayContains(ar...
答案function capitalizeFirstLetter(string) { return string.charAt(0).toUpperCase() + string.slice(1); } 其他一些答案修改了String.prototype (这个答案也是如此),但由于可维护性,我现在建议不要这样做(很难找到函数添加到prototype ,如果其他代码使用相同的函数,可能会导致冲突) name / a browser 将来添加一...
JavaScript: Check if First Letter of a String is Upper Case, Capitalizing the First Letter If we found out that the first letter of the string is lower case, and if we want to capitalize it, we can do that using following method: function capitalizeFirstLetter(word) { return word.charAt...
function capitalizeFirstLetter(string) { return string.charAt(0).toUpperCase() + string.slice(1); } let myString = "hello, world!"; let capitalized = capitalizeFirstLetter(myString); console.log(capitalized); // 输出: Hello, world!
function capitalizeFirstLetter(sentence) { var trimmedSentence = sentence.trim(); var firstChar = trimmedSentence.charAt(0); var capitalizedFirstChar = firstChar.toUpperCase(); var remainingChars = trimmedSentence.slice(1); return capitalizedFirstChar.concat(remainingChars); } var sentence = "hello...
function capitalizeFirstLetter(string) { return string.charAt(0).toUpperCase() + string.slice(1); } console.log(capitalizeFirstLetter('foo')); // Foo 其他一些答案修改 String.prototype (这个答案也曾经使用过),但由于可维护性,我现在建议不要这样做(很难找出函数被添加到 prototype 如果其他代码使用...
You can capitalize the first letter of a string by using the charAt() and slice() methods in JavaScript. function capitalizeFirstLetter(str) { return str.charAt(0).toUpperCase() + str.slice(1); } let myString = "codedamn"; console.log(capitalizeFirstLetter(myString)); // Outputs: Code...
// Define a function named capitalize_First_Letter that takes a string parameter (text) function capitalize_First_Letter(text) { // Split the input string into an array of words let words = text.split(" "); // Iterate through each word in the array for (let i = 0; i < words....