@rbxts/jsnatives
toLowerCase
Converts a string to lowercase.
Signature
function toLowerCase(str: string): string
Description
The toLowerCase
function converts all the alphabetic characters in a string to lowercase. It uses Lua's string.lower
function internally.
Parameters
str
: The string to convert to lowercase.
Return value
- A new string with all alphabetic characters converted to lowercase.
Examples
Basic usage
// Convert a string to lowercaseconst lower = String.toLowerCase("HELLO WORLD");print(lower); // Outputs: "hello world"
// Mixed case conversionconst mixed = String.toLowerCase("Hello World");print(mixed); // Outputs: "hello world"
Case preservation for non-alphabetic characters
// Numbers and special characters are not affectedconst withNumbers = String.toLowerCase("HELLO 123!");print(withNumbers); // Outputs: "hello 123!"
// Only alphabetic characters are convertedconst special = String.toLowerCase("ABC-DEF_GHI@JKL");print(special); // Outputs: "abc-def_ghi@jkl"
Handling edge cases
// Empty stringconst empty = String.toLowerCase("");print(empty); // Outputs: ""
// Already lowercaseconst alreadyLower = String.toLowerCase("hello world");print(alreadyLower); // Outputs: "hello world"
// Non-alphabetic charactersconst nonAlpha = String.toLowerCase("123!@#");print(nonAlpha); // Outputs: "123!@#" (unchanged)
Practical applications
// Case-insensitive comparisonfunction compareIgnoreCase(str1: string, str2: string): boolean { return String.toLowerCase(str1) === String.toLowerCase(str2);}
print(compareIgnoreCase("Hello", "hello")); // Outputs: trueprint(compareIgnoreCase("World", "earth")); // Outputs: false
// Normalizing user inputfunction normalizeInput(input: string): string { return String.toLowerCase(input);}
print(normalizeInput("John.Doe@Example.COM")); // Outputs: "john.doe@example.com"
Using with other string functions
// Combine with other string functions for more complex operationsfunction isYesResponse(response: string): boolean { const normalized = String.toLowerCase(String.trim(response)); return normalized === "yes" || normalized === "y";}
print(isYesResponse(" YES ")); // Outputs: trueprint(isYesResponse("y")); // Outputs: trueprint(isYesResponse("No")); // Outputs: false
// Case-insensitive searchfunction containsIgnoreCase(text: string, search: string): boolean { return String.includes(String.toLowerCase(text), String.toLowerCase(search));}
print(containsIgnoreCase("Hello World", "world")); // Outputs: trueprint(containsIgnoreCase("JavaScript", "SCRIPT")); // Outputs: true