string.lower()

The string.lower() function in Lua is used to convert all uppercase characters in a given string to lowercase. This function is useful for case-insensitive comparisons and standardizing string formats.

Syntax

string.lower(s)

Parameters

  • Name
    s
    Type
    string
    Description

    The input string that will be converted to lowercase.

Return Value

Returns a new string with all uppercase characters converted to lowercase. The original string remains unchanged.

Examples

Example 1: Basic Conversion

Convert a string with mixed case to lowercase.

Example 1

local text = "Hello, Lua!"

local lowerText = string.lower(text)

print(lowerText)  -- Output: hello, lua!

Example 2: String with All Uppercase Letters

Convert an all-uppercase string to lowercase.

Example 2

local text = "THIS IS A TEST."

local lowerText = string.lower(text)

print(lowerText)  -- Output: this is a test.

Example 3: String with Special Characters

Special characters remain unchanged during conversion.

Example 3

local text = "Lua is Fun! 123 #$%"

local lowerText = string.lower(text)

print(lowerText)  -- Output: lua is fun! 123 #$%

Example 4: Case-Insensitive Comparison

Use string.lower() for case-insensitive string comparison.

Example 4

local str1 = "OpenAI"
local str2 = "openai"

if string.lower(str1) == string.lower(str2) then
  print("The strings are equal (case-insensitive).")
else
  print("The strings are not equal.")
end

-- Output: The strings are equal (case-insensitive).

Use Cases

  • Normalization: Standardize strings before processing or storing them.
  • Search Functionality: Implement case-insensitive search features.
  • Data Cleaning: Prepare user input for comparison by converting to a common case.

See also

  • string.upper(): Converts all lowercase characters in a string to uppercase, often used in conjunction with string.lower() for case normalization.
  • string.gsub(): Can be used to replace parts of a string after standardizing its case with string.lower().

Was this page helpful?

Table of Contents