We have a string.toLowerCase() and string.toUpperCase() in Javascript, but no out of the box function to convert to title case. Fear not if you are in that pickle - it is quite easy.
|
|
ConvertTitleCase
takes one argument, which is a string. The string is split into distinct word array (split at ‘space’), and each word is then converted to initial cap.
The below statement converts first char of the word to upper case, and joins the upper case letter with rest of the chars in the word.
|
|
The changed array is joined back to form a string with a space in between words. The string with title case is then returned to the caller.