10 examples of 'tocapitalize js' in JavaScript

Every line of 'tocapitalize js' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
28function uncapitalize(str) {
29 return (str.charAt(0).toLowerCase() + str.slice(1));
30}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

7function capitalize(str) {
8 return str && str[0].toUpperCase() + str.slice(1);
9}
145function capitalize(string) {
146 return string.replace(/^[a-z]/, function(str) {
147 return str.toUpperCase();
148 });
149}
21export function capitalize(str) {
22 return ucFirst(str);
23}
1export default function capitalize(str) {
2 return str.replace(/\b\w/g, function (l) {
3 return l.toUpperCase()
4 });
5}
63function capitalize(str) {
64 return str.replace(/^[a-z]/, char => char.toUpperCase());
65}
1export default function capitalize(str: string) {
2 return `${str.slice(0, 1).toUpperCase()}${str.slice(1)}`;
3}
2function capitalize (str) {
3 // spit the string to array
4 let wordArr = str.split(' ');
5
6 // loop through each element of array and capitalize the first letter
7
8 for (let i=0; i<wordArr.length; i++) {
9
10 // Some people might proceed like this, but strings are immutable
11 // let currentWord = wordArr[i];
12 // currentWord[0] = currentWord[0].toUpperCase();
13 // console.log(currentWord);
14
15 // Append the first letter (capitalized)
16 let currentWord = '';
17 currentWord += wordArr[i][0].toUpperCase();
18
19 // Append the rest of the word (can be easily done by String slice)
20 for (let j=1; j<wordArr[i].length; j++) {
21 currentWord += wordArr[i][j];
22 }
23
24 // replace the current word by currentWord
25 wordArr[i] = currentWord;
26 }
27
28 // join the array into string
29 return wordArr.join(' ');
30}
7export function capitalize (string) {
8 return string && (string.charAt(0).toUpperCase() + string.slice(1))
9}
22function _capitalize(string) {
23 return string && (string.charAt(0).toUpperCase() + string.slice(1));
24}

Related snippets