string.rep() / string.reverse()
string.rep() and string.reverse() are functions in Lua's standard string library. string.rep() repeats a string a given number of times and concatenates the copies; string.reverse() returns the byte-level reverse of a string. Both can be called using either function notation or method notation.
Syntax
-- ----------------------------------------------- -- string.rep(s, n [, sep]) -- ----------------------------------------------- string.rep(s, n) -- returns s repeated n times string.rep(s, n, sep) -- returns n copies of s with sep inserted between each copy s:rep(n) -- method notation s:rep(n, sep) -- method notation with separator -- ----------------------------------------------- -- string.reverse(s) -- ----------------------------------------------- string.reverse(s) -- returns the byte-level reverse of s s:reverse() -- method notation
Function Reference
| Function | Description |
|---|---|
string.rep(s, n) | Returns a string formed by concatenating n copies of s. Returns an empty string when n is 0 or less. |
string.rep(s, n, sep) | Returns n copies of s with the separator sep inserted between each copy. |
s:rep(n) | Method notation for string.rep(s, n). Can be called directly on a string value. |
s:rep(n, sep) | Method notation for string.rep(s, n, sep). |
string.reverse(s) | Returns a string whose byte sequence is the reverse of s. Multi-byte characters (UTF-8, etc.) are reversed at the byte level, so Japanese text should be used with care. |
s:reverse() | Method notation for string.reverse(s). |
Sample Code
sg_string_rep_reverse.lua
-- sg_string_rep_reverse.lua — sample for string.rep() and string.reverse()
-- Uses Steins;Gate characters to demonstrate each function
-- -----------------------------------------------
-- string.rep() — repeat a string
-- -----------------------------------------------
local name = "Okabe Rintaro"
-- Basic repetition (concatenates n copies)
local repeated = string.rep(name, 3)
print("Repeated (3 times): " .. repeated)
-- Repeat with a separator
local separated = string.rep(name, 3, " / ")
print("With separator: " .. separated)
-- Method notation produces the same result
local method_rep = name:rep(2, " & ")
print("Method notation: " .. method_rep)
-- n = 0 returns an empty string
local empty = string.rep("Makise Kurisu", 0)
print("n=0 result: \"" .. empty .. "\" (empty string)")
print("")
-- -----------------------------------------------
-- Formatted list using separator
-- -----------------------------------------------
local members = { "Okabe Rintaro", "Makise Kurisu", "Shiina Mayuri", "Hashida Itaru", "Amane Suzuha" }
-- Build a member list manually with a separator
local separator = " | "
local line = ""
for i, member in ipairs(members) do
if i == 1 then
line = member
else
line = line .. separator .. member
end
end
print("Lab members: " .. line)
-- Generate a border with string.rep()
local border = string.rep("-", 40)
print(border)
print("")
-- -----------------------------------------------
-- string.reverse() — reverse a string
-- -----------------------------------------------
local code = "SGLAB001"
-- Reverses the byte sequence (works correctly for ASCII)
local reversed = string.reverse(code)
print("Original string: " .. code)
print("Reversed: " .. reversed)
-- Method notation works the same way
local mayuri_id = "MAYURI"
print("Method notation: " .. mayuri_id:reverse())
print("")
-- -----------------------------------------------
-- Palindrome check (applying string.reverse())
-- -----------------------------------------------
local function is_palindrome(s)
return s == string.reverse(s)
end
local words = { "racecar", "SGLAB", "level", "kurisu", "madam" }
print("--- Palindrome check ---")
for _, word in ipairs(words) do
local result = is_palindrome(word) and "palindrome" or "not a palindrome"
print(" " .. word .. ": " .. result)
end
print("")
-- -----------------------------------------------
-- Generating table borders with string.rep()
-- -----------------------------------------------
print("--- Lab member power ranking ---")
local header_border = "+" .. string.rep("-", 16) .. "+" .. string.rep("-", 10) .. "+"
local members_data = {
{ name = "Okabe Rintaro", power = 9999 },
{ name = "Makise Kurisu", power = 9800 },
{ name = "Amane Suzuha", power = 9500 },
{ name = "Shiina Mayuri", power = 9100 },
{ name = "Hashida Itaru", power = 8800 },
}
print(header_border)
for _, m in ipairs(members_data) do
print(string.format("| %-14s | %8d |", m.name, m.power))
end
print(header_border)
lua sg_string_rep_reverse.lua Repeated (3 times): Okabe RintaroOkabe RintaroOkabe Rintaro With separator: Okabe Rintaro / Okabe Rintaro / Okabe Rintaro Method notation: Okabe Rintaro & Okabe Rintaro n=0 result: "" (empty string) Lab members: Okabe Rintaro | Makise Kurisu | Shiina Mayuri | Hashida Itaru | Amane Suzuha ---------------------------------------- Original string: SGLAB001 Reversed: 100BALGS Method notation: IRUYAM --- Palindrome check --- racecar: palindrome SGLAB: not a palindrome level: palindrome kurisu: not a palindrome madam: palindrome --- Lab member power ranking --- +----------------+----------+ | Okabe Rintaro | 9999 | | Makise Kurisu | 9800 | | Amane Suzuha | 9500 | | Shiina Mayuri | 9100 | | Hashida Itaru | 8800 | +----------------+----------+
Common Mistakes
Not knowing that passing 0 as the repeat count returns an empty string
When n is 0 or less, string.rep(s, n) returns an empty string "" without raising an error. Because no error is produced, this can silently introduce a bug.
-- n = 0 returns an empty string
local result = string.rep("Okabe Rintaro", 0)
print(#result) -- 0 (empty string)
print(result == "") -- true
-- OK: check the value of n before calling
local count = 0
if count > 0 then
print(string.rep("Okabe Rintaro", count))
else
print("(no repetition)")
end
Multi-byte strings are corrupted when reversed with string.reverse
string.reverse() reverses the byte sequence. Japanese characters in UTF-8 span multiple bytes, so reversing at the byte level scrambles each character's byte sequence, producing garbled output.
-- NG: reversing a Japanese string with string.reverse produces garbled output local name = "牧瀬紅莉栖" local reversed = string.reverse(name) print(reversed) -- garbled output
-- OK: string.reverse works correctly on ASCII strings local code = "KURISU" print(string.reverse(code)) -- USIRUК (ASCII is 1 byte per character, reversal is correct) -- Reversing Japanese strings requires a separate library that works at the codepoint level
Not knowing the third separator argument of string.rep
Since Lua 5.2, string.rep(s, n, sep) accepts a third argument sep that is inserted between each copy. Not knowing about it leads to writing manual loops for the same effect.
-- NG: implementing separator-joined repetition with a manual loop
local parts = {}
for i = 1, 5 do
parts[i] = "ElPsyKongroo"
end
print(table.concat(parts, " | ")) -- works but verbose
-- OK: use the third argument for the separator
print(string.rep("ElPsyKongroo", 5, " | "))
-- ElPsyKongroo | ElPsyKongroo | ElPsyKongroo | ElPsyKongroo | ElPsyKongroo
Overview
string.rep(s, n) returns a string formed by concatenating n copies of s. When the third argument sep is given, it is inserted between each copy. When n is 0 or less, an empty string is returned. The function is handy for generating borders and separator-joined lists without writing loops.
string.reverse(s) returns a new string whose byte sequence is the reverse of s. For ASCII strings, this produces an intuitive character-level reversal. However, multi-byte encodings such as UTF-8 are reversed at the byte level, so passing a string that contains Japanese or emoji will produce garbled output. Reversing Unicode text correctly requires splitting the string into codepoints before reversing and rejoining.
Both functions support method notation (s:rep(n), s:reverse()). In Lua, all strings share the string table as their metatable, so function notation and method notation are fully equivalent. See also string.format() and string.find() / string.match().
If you find any errors or copyright issues, please contact us.