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
local result = string.reverse("hello")
engine.print("info", result) --'olleh'local result = string.reverse("hello world")
engine.print("info", result) --'dlrow olleh'local result = string.reverse("12345")
engine.print("info", result) --'54321'local result = string.reverse("a")
engine.print("info", result) --'a'local result = string.reverse("")
engine.print("info", result) --''local function isPalindrome(str)
return str == string.reverse(str)
end
engine.print("info", isPalindrome("racecar")) --true
engine.print("info", isPalindrome("hello")) --falselocal result = string.upper(string.reverse("hello"))
engine.print("info", result) --'OLLEH'local result = string.reverse("hello!")
engine.print("info", result) --'!olleh'local original = "test"
local reversed = string.reverse(string.reverse(original))
engine.print("info", reversed == original) --truelocal sentence = "hello world"
local reversed = string.gsub(sentence, "%S+", function(word)
return string.reverse(word)
end)
engine.print("info", reversed) --'olleh dlrow'local text = "ABC"
local mirror = text.."|"..string.reverse(text)
engine.print("info", mirror) --'ABC|CBA'local binary = "1010"
local reversed = string.reverse(binary)
engine.print("info", reversed) --'0101'local num = '123456'
engine.print("info", string.reverse(num)) --'654321'local data = 'ABCDEF'
for i = 1, #data do
local char = string.sub(string.reverse(data), i, i)
engine.print("info", char)
end
--[[
Output:
'F'
'E'
'D'
'C'
'B'
'A'
]]