How would I remove characters from a string until the index reaches a certain character

How would I remove characters from a string until the first letter of the string is equal to a certain character

str = "aDajD:1: Wow"
print("starting")

repeat wait() str = str:sub(0,1) until str:sub(0,1) == ":"

print("ended")

I tried this above but then it just never ended, I want all the characters before the :'s to be removed so it’s just “:1: Wow” but the “aDajD” at the start will be randomized so I can’t just use gsub, and it’s at a randomized length aswell so you can’t exactly sub it either

And yes I realised that the code would never reach “:” because I set it to the first character

You can still use gsub:

string.gsub(str, "(.+):")

might work

For that, you do not need to have a loop to achieve your desired outcome. Instead you can find the index of the occurrence of the string you want to be the first letter:

local firstChar = ":"

local function Sub(str)
   return string.sub(str, string.find(str, firstChar), -1)
end

That should achieve your desired outcome

1 Like

It almost works but it removed the :1: part aswell which I want to keep

1 Like

Thankyou this worked perfectly

You also technically don’t need sub you could do:

local split = string.split(str, ":")
local str2 = split[2]
print(str2)

If you read the post carefully, he wants the colon to be the first character of the string. Using string.split will return the characters after the colon which is not desired. Also, in a string like this: “aDajD:1: Wow”, your method will return “1” since there’s another colon in the string which means string.sub is the way to go

1 Like

Single operations are going to be more efficient.

local s = "aDajD:1: Wow"
s = string.match(s, ":.*")
print(s) --:1: Wow
1 Like