Current Size:
Chakra BreakPoints
base0px
sm480px
md768px
lg992px
xl1280px
2xl1536px
Current Height:Width
widthpx
heightpx

Subjects

Common String Manipulation in JavaScript

Date Created: 2022/09/20

Last Update: 2022/12/05

#javascript #cheatsheet #reference #string

Uppercase First Letter

Single Word

Solution One

const a = "enterprise"
const name = `${a.slice(0, 1).toUpperCase()}${a.slice(1)}`
// Enterprise

or

const a = "starship"
const strArr = a.split("")
const newStr = strArr[0].toUpperCase() + strArr.slice(1).join("")
// Startship

or

const a = "mountain"
const strArr = a.split("")
const newStr = [strArr[0].toUpperCase(), ...strArr.slice(1)].join("")
// Mountain

Multiple Words

const capitalizeCase = (string) => {
const cleanStr = string.replaceAll(/^[a-zA-Z0-9]*$/gi, "")
const splitOn = new RegExp("[-_ ]", "i")
const stg = cleanStr.split(splitOn)
const stringArr = []
stg.forEach((s) => {
const t = s.split("")[0]
if (t != null) {
const n = [t.toUpperCase(), ...s.slice(1)].join("")
stringArr.push(n)
}
})
const result = stringArr.join(" ")
return result
}

TODO: add 👇

const camelCase = (string) => {
/**
* "theQuickBrownFoxJumpsOverTheLazyDog"
*/
}
const kebabCase = (string) => {
/**
* "the-quick-brown-fox-jumps-over-the-lazy-dog"
*/
}
const snakeCase = (string) => {
/**
* "the_quick_brown_fox_jumps_over_the_lazy_dog"
*/
}

More Notes

All Notes
HomeProjects

Links

Home Articles Notes Projects About Style Guide Site Credits

Contact

 [email protected]

Location

🌎 Earth