代码语言:txt 复制 function capitalizeFirstLetter(sentence) { var trimmedSentence = sentence.trim(); var firstChar = trimmedSentence.charAt(0); var capitalizedFirstChar = firstChar.toUpperCase(); var remainingChars =
Output: “This is a test” Uppercase first letter in a string usinf RegEx You can capitalize the first letter of a string by using RegEx also. The following program shows how to change the first letter of a string to upper case using RegEx. The following function change the first letter...
代码语言:javascript 运行 AI代码解释 function capitalizeFirstLetter(str) { return str.charAt(0).toUpperCase() + str.slice(1); } function capitalizeAllWords(str) { var words = str.split(' '); return words.map(capitalizeFirstLetter).join(' '); } capitalizeAllWords('hello world'); 收藏分享...
2. Capitalize the first letter of each word The key is to find the first letter of each word // my name is epeli const titleize = (str) => { return str.toLowerCase().replace(/(?:^|\s)\w/g, (c) => c.toUpperCase()) } console.log(titleize('my name is epeli')) // My...
为了提高开发效率并编写更清晰、可维护的代码,程序员们经常使用各种自定义函数。今天给大家介绍20个常用的JavaScript函数,希望对大家有帮助。
CapitalizeCapitalizes the first letter of a string.Use destructuring and toUpperCase() to capitalize first letter, ...rest to get array of characters after first letter and then Array.join('') to make it a string again. Omit the lowerRest parameter to keep the rest of the string intact, ...
Capitalize the First letter of Sentences Uppercase or Lowercase Text Remove Spaces Online Replace Tabs with Spaces Capitalize Each Word Remove Numbers from Text SEO and Word Tools Word Counter Sentence Counter Online Character Counter Title Capitalization Tool Meta Description Length Checker Content Convers...
CapitalizeCapitalizes the first letter of a string.Use destructuring and toUpperCase() to capitalize first letter, ...rest to get array of characters after first letter and then Array.join('') to make it a string again. Omit the lowerRest parameter to keep the rest of the string intact, ...
Node // CommonJSconst_30s =require('30-seconds-of-code'); _30s.average(1,2,3);// ES Modulesimport_30sfrom'30-seconds-of-code'; _30s.average(1,2,3); To import snippets directly: // CommonJSconst{ average } =require('30-seconds-of-code'); ...
functioncapitalizeFirstLetter(str){returnstr.charAt(0).toUpperCase()+str.slice(1);} 19. 判断数组是否包含特定元素 在处理数组时,你可能需要确定数组中是否包含特定元素。arrayContains函数接受一个数组和一个要查找的元素作为参数,然后返回一个布尔值,指示数组是否包含该元素。