How to edit a specific part in a string?

I was wondering, how I could edit a specific part of a string.

Such as Hello! I am bob. Hehe! and change the bob to Aiden. I was wondering how I could do that.

Lua strings are immutable so they can’t be modified.

You can use string.gsub.

print(string.gsub("Hello world", "Hello", "Goodbye")) -- Goodbye world 1

string.gsub returns two results: the first one being the new string. The second one being how many replacements it made. Which is why you see a 1. Because it made only 1 replacements. This method is much more powerful but for your use case this is really all you need.

2 Likes