@rbxts/jsnatives
toTitleCase
Converts a string to Title Case format.
Signature
function toTitleCase(str: string): string
Description
The toTitleCase
function converts a string to Title Case format, where the first letter of each word is capitalized and the remaining letters are lowercase. Words are typically identified by spaces, but the function also handles other separators like hyphens and underscores.
Parameters
str
: The string to convert to Title Case.
Return value
- A new string in Title Case format.
Examples
Basic usage
// Converting different formats to Title Caseconst space = String.toTitleCase("hello world");print(space); // Outputs: "Hello World"
const camel = String.toTitleCase("helloWorld");print(camel); // Outputs: "Hello World"
const pascal = String.toTitleCase("HelloWorld");print(pascal); // Outputs: "Hello World"
const snake = String.toTitleCase("hello_world");print(snake); // Outputs: "Hello World"
const kebab = String.toTitleCase("hello-world");print(kebab); // Outputs: "Hello World"
Multiple words
// Converting multi-word stringsconst multiple = String.toTitleCase("hello beautiful world");print(multiple); // Outputs: "Hello Beautiful World"
const mixedSeparators = String.toTitleCase("hello_beautiful-world");print(mixedSeparators); // Outputs: "Hello Beautiful World"
Handling edge cases
// Empty stringconst empty = String.toTitleCase("");print(empty); // Outputs: ""
// Single wordconst single = String.toTitleCase("hello");print(single); // Outputs: "Hello"
// All uppercaseconst uppercase = String.toTitleCase("HELLO WORLD");print(uppercase); // Outputs: "H E L L O W O R L D" (each uppercase is treated as new word)
// Already in Title Caseconst alreadyTitle = String.toTitleCase("Hello World");print(alreadyTitle); // Outputs: "Hello World"
Converting from other cases
// From sentence caseconst sentence = String.toTitleCase("this is a sentence.");print(sentence); // Outputs: "This Is A Sentence."
// From camelCaseconst camel = String.toTitleCase("userFirstName");print(camel); // Outputs: "User First Name"
// From PascalCaseconst pascal = String.toTitleCase("UserLoginRequest");print(pascal); // Outputs: "User Login Request"
// From snake_caseconst snake = String.toTitleCase("user_first_name");print(snake); // Outputs: "User First Name"
// From kebab-caseconst kebab = String.toTitleCase("api-response-code");print(kebab); // Outputs: "Api Response Code"
Practical applications
// Format titles for displayfunction formatTitle(title: string): string { return String.toTitleCase(title);}
print(formatTitle("the lord of the rings")); // Outputs: "The Lord Of The Rings"print(formatTitle("harry_potter_and_the_chamber_of_secrets")); // Outputs: "Harry Potter And The Chamber Of Secrets"