String
string.reverse
Shared
Returns a string with characters in reverse order
Syntax
local result = string.reverse(input)Parameters
| Type | Name | Description |
|---|---|---|
string | input | String to be reversed |
Returns
| Type | Name | Description |
|---|---|---|
string | result | String with all characters reversed |
Examples
--Reverse a word
local result = string.reverse("hello")
engine.print(result) --olleh--Reverse a sentence
local result = string.reverse("hello world")
engine.print(result) --dlrow olleh--Reverse numbers
local result = string.reverse("12345")
engine.print(result) --54321--Single character (no change visually)
local result = string.reverse("a")
engine.print(result) --a--Empty string
local result = string.reverse("")
engine.print(result) ----Check if string is palindrome
local function isPalindrome(str)
return str == string.reverse(str)
end
engine.print(isPalindrome("racecar")) --true
engine.print(isPalindrome("hello")) --false--Reverse and uppercase
local result = string.upper(string.reverse("hello"))
engine.print(result) --OLLEH--Reverse with special characters
local result = string.reverse("hello!")
engine.print(result) --!olleh--Double reverse returns original
local original = "test"
local reversed = string.reverse(string.reverse(original))
engine.print(reversed == original) --true--Reverse each word separately
local sentence = "hello world"
local reversed = string.gsub(sentence, "%S+", function(word)
return string.reverse(word)
end)
engine.print(reversed) --olleh dlrow--Create mirror effect
local text = "ABC"
local mirror = text.."|"..string.reverse(text)
engine.print(mirror) --ABC|CBA--Reverse binary string
local binary = "1010"
local reversed = string.reverse(binary)
engine.print(reversed) --0101--With numbers as strings
local num = "123456"
engine.print(string.reverse(num)) --654321--Process from end to start
local data = "ABCDEF"
for i = 1, #data do
local char = string.sub(string.reverse(data), i, i)
engine.print(char) --F, E, D, C, B, A
end