How to use String.gsub?

I want to know how to use string.gsub

I do not know how whatsoever.

developer.roblox.com and devforum.roblox.com and youtube.com

I know this is dumb but I do not know how to use string.gsub whatsoever all I know about it is it can be used on strings. So I have looked everywhere and there was no good info I could fine. I would love to know how to use string.gsub/:gsub please. I do not understand why people add stuff like "+%*" or "%@)"

local MyMessage = "My message Test"
--// I do not know the rest
6 Likes

The basic usage of it would be when you have a string and want to change some word/letter in it to something else. In this case i have a string “Hello Kid!”, i want to change the word “Kid” to “World”. Its done like this

local String = "Hello Kid!"
local newstring = string.gsub(String, "Kid", "World")
print(newstring) -- prints - Hello World!

There also other things you can do with it, but i’m not using it so widely so i can’t help with explaining those.

You can read more about it at the official API thought - string | Roblox Creator Documentation

20 Likes

I am on the documents rn it isn’t helping and thank you for this one example!!

1 Like

You can edit a part of a string with string.gsub.
This example will remove the numbers using patterns.

local str = 'This is a example. Hello1234'
str = string.gsub(str, '%d+', '')

print(str) --> This is a example. Hello
1 Like

???

2 Likes

String patterns can be pretty confusing, but that is a character class, bascially, a set of characters you search for.

%d+

This pattern in particular will do 2 things. %d states that you should look for any digits in a specific string. The + modifier states that the character class should match 1 or more of said class.

The developer API has a page on this, its a good read.

8 Likes

%d is a character class, it represents a digit. 0,1,2,3,4,5,6,7,8,9

So matching the string for %d would only replace the first digit it finds in the string and stop there.

the + is a modifier, it tells roblox to look for every other character further down the string that matches the class and replace it too.

So “%d+” would look for the first digit in the string, replace it, and then go on and look for all other digits after it and replace them too.

Read this, it wont be easy to understand at first but you’ll get it eventually. String Patterns

I hope this helped.

4 Likes

So for e.g.

local message = "Hello I am roblox! and roblox again"
local stringNew = message:gsub("roblox","%d+")

print(stringNew) --// Prints roblox roblox?

thanks for the example, helped me figure out a neat text effect

I’ll provide another example since your confused.
The other people explained what that is.

local str = 'Hello world!'
local new = string.gsub(str, ' ', '_')
print(new) --> Hello_world!

This wouldn’t work since your trying to replace a word with a pattern.
I’m pretty sure this would error.

print(('john ate 1 egg'):gsub('john ate %w egg', 'jane ate 2 eggs'))