Is there a simple way to convert a string to Title Case? E.g. john smith
becomes John Smith
. I'm not looking for something complicated like John Resig's solution, just (hopefully) some kind of one- or two-liner.
Java String toLowerCase() MethodThe toLowerCase() method converts a string to lower case letters. Note: The toUpperCase() method converts a string to upper case letters.
To capitalize an entire string, simply call . toUpperCase() on the string: let myString = 'alligator'; myString.
Try this:
function toTitleCase(str) { return str.replace( /\w\S*/g, function(txt) { return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); } ); }
<form> Input: <br /><textarea name="input" onchange="form.output.value=toTitleCase(this.value)" onkeyup="form.output.value=toTitleCase(this.value)"></textarea> <br />Output: <br /><textarea name="output" readonly onclick="select(this)"></textarea> </form>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With