HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to LuaVariablesNumbers & MathStringsTablesConditions (if)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Table
🔁 Condition & Loop
🧩 Function
Lua›Strings

Strings

#s length, string.upper, string.sub (1-indexed) and string.find.

Simple enough for a 5-year-old to understand

Length and uppercase

Example code
1
local s = "hello"
2
print(#s)
Length: 7
Output / InputLength: 7
3
print(string.upper(s))
HELLO
Output / InputHELLO
Output / Input

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the text "hexlibrary" in UPPERCASE.

🎯 Expected output: HEXLIBRARY

Loading editor…

About this topic

Strings are immutable in Lua. When you manipulate a string, a new string is created while the old one remains unchanged. Strings with identical content are stored only once in memory, which makes comparisons very fast.

The concatenation operator is two dots. This operator automatically converts numbers to strings, so you can easily concatenate a string with a number. However, trying to concatenate with nil causes an error — this is one of the most common Lua errors.

String functions are in the string library and their indices start at one. A negative index means counting from the back; minus one is used to get the last character. Together, these two rules make Lua's string operations concise and readable.

What you will learn

  • Creating and concatenating strings
  • Measuring length
  • Extracting substrings and searching
  • Using negative indices
  • Generating formatted output

Who is this for?

  • Script writers who will generate in-game messages
  • Those who will perform text processing
  • Those getting nil concatenation errors

When to use?

Every message printed to the screen in game plugins is generated via string concatenation; the formatted output function improves readability.

If you are going to concatenate a large number of pieces inside a loop, collecting them in a table and joining them all at once at the end is noticeably faster.

Common mistakes

Wrong

local ad
print("Hello " .. ad)

Since the variable name has not been assigned a value, it is nil, and nil cannot be concatenated: you get the "attempt to concatenate a nil value" error. This is the most common runtime error in Lua and is usually caused by a typo or an expected value not being received.

Correct

local ad = "Ayşe"
print("Merhaba " .. ad)

Wrong

local metin = "hello"
print(string.sub(metin, 0, 3))

In Lua, indices start at one; zero is not a valid position and you will get unexpected results. People coming from other languages are used to counting from zero, and this difference manifests itself everywhere in Lua.

Correct

local metin = "hello"
print(string.sub(metin, 1, 3))

Wrong

local metin = "hello"
metin:upper()
print(metin)

Since strings are immutable, the method does not modify the original, it returns a new string. Because you didn't assign the result anywhere, the lowercase version is still printed to the screen. Ignoring the return value is a silent error.

Correct

local metin = "hello"
metin = metin:upper()
print(metin)

Frequently asked questions

How do I measure string length?

The hash sign gives the number of bytes in the string. Note: Since Turkish characters take up more than one byte, the letter count and the byte count can be different. Unicode awareness requires an additional library.

How do I search within a string?

string.find returns the position, and string.match returns the matching part. Lua has its own pattern syntax; it is similar to regular expressions but not the same. Pattern characters are fewer, but they handle most everyday tasks.

How do I print formatted text?

The string.format function works with placeholders and allows you to control the number of decimal places and alignment. Compared to long concatenation chains, it is both more readable and reduces formatting errors.

Summary

Concatenation is done with two dots.

Concatenating with nil causes a runtime error.

Indices start at one, and negative indices count from the end.

Strings are immutable, methods return a new string.

string.format produces formatted output.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code