Lua .char()

StevenSwiniarski's avatar
Published Sep 7, 2023Updated Sep 13, 2023
Contribute to Docs

In Lua, the string.char() function is used to convert a decimal value to its internal character value.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Lua, a general-purpose programming language used for building games, web apps, and developer tools.
    • Beginner Friendly.
      4 hours

Syntax

string.char(I)

In the above syntax, the identifier I represents the decimal value which will be converted into a character.

Example 1

The following example uses the string.char() function to convert different decimal values to their associated character values:

x = 10
i = 0
while i <= x do
s = string.char(97 + i)
print(s)
i = i + 1
end

This example results in the following output:

a
b
c
d
e
f
g
h
i
j
k

Example 2

The string.char() function can take multiple arguments as well:

i = 97
s = string.char(i, i+1, i+2, i+3)
print(s)

This example results in the following output:

abcd

All contributors

Contribute to Docs

Learn Lua on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Lua, a general-purpose programming language used for building games, web apps, and developer tools.
    • Beginner Friendly.
      4 hours