String editing

Just wondering, is there a way or function in Defold that is able to take a string and edit it?

For example, say I have a string "bonkeronis", and I want to get rid of "eronis" at the end. Is there anything I could do for that?

There are a lot of functions to change strings in Lua; which one to use depends on how you want to do it.

Just removing the last 6 characters:

local my_string = "bonkeronis"

-- These two mean the same thing
local shortened = string.sub(my_string, 1, -7)
local shortened = my_string:sub(1, -7)

Replacing all occurrences of "eronis" with a blank string:

local shortened = my_string:gsub("eronis", "")

Or if you want to get fancy, you can replace "eronis" only if it’s at the end of the string:

local shortened = my_string:gsub("eronis$", "")

More info:

8 Likes