Opposite of ..=

Recently I found out that ..= can be used to add a string to a string(?)

So like:

local STRING = "Hi"
STRING ..= " Player"
print(STRING) -- "Hi Player"

and now I want to find out the opposite of ..= so it’d do:

local STRING = "Hi Player"
STRING (whatever) " Player"
print(STRING) -- "Hi"

If this isn’t possible/doesn’t exist, are there any ways of achieving a similar effect?

1 Like

You could use string.gsub.
Here’s an example:
STRING = string.gsub(STRING, "Player", "")
In the above example, you replace “Player” with “” aka nothing.

You can use string.split to do this.
You separate a string using a “splitter”, which then makes a table with the two pieces.

local message = "Hello world!"
local split = string.split(message," ")
print(split)
--{"Hello",
--    "world!"}

To remove x characters from the last part of the string:

local x = 4
local str = "Hi Player"
str = str:sub(1, str:len()-x)

To remove x characters from the start:

str = str:sub(x+1, str:len())

To remove all occurrences of string a from str:

local a = "Player"
str = str:gsub(a, "")

To remove all occurrences of a, x times from the start:

str = str:gsub(a, "", x)

To remove all occurrences of a, x times from the end:

str = str:reverse():gsub(a, "", x):reverse()

Best of luck!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.