string.upper()
The string.upper()
function in Lua is used to convert all lowercase letters in a string to their uppercase counterparts. This function is useful for text formatting, normalization, or comparison purposes where case sensitivity may be an issue.
Syntax
string.upper(s)
Parameters
- Name
s
- Type
- string
- Description
The string to be converted to uppercase.
Return Value
Returns a new string with all lowercase letters converted to uppercase. Non-letter characters remain unchanged.
Examples
Example 1: Basic Uppercase Conversion
Converting a simple string to uppercase.
Example 1
local result = string.upper("hello world")
print(result) -- Output: HELLO WORLD
Example 2: String with Mixed Case
Handling a string with mixed case letters.
Example 2
local result = string.upper("Lua Programming")
print(result) -- Output: LUA PROGRAMMING
Example 3: Non-Letter Characters
Demonstrating that non-letter characters are unchanged.
Example 3
local result = string.upper("hello, world! 123")
print(result) -- Output: HELLO, WORLD! 123
Example 4: Empty String
Behavior when passing an empty string.
Example 4
local result = string.upper("")
print(result) -- Output: (empty string)
Use Cases
- Text Normalization: Ensuring uniformity in string comparisons.
- Formatting: Preparing strings for display by converting them to uppercase.
See also
string.lower()
: Converts all uppercase letters in a string to lowercase, useful for case-insensitive comparisons.