Forum

> > CS2D > Scripts > String Replace
Forums overviewCS2D overview Scripts overviewLog in to reply

English String Replace

4 replies
To the start Previous 1 Next To the start

old String Replace

sixpack
User Off Offline

Quote
Hello, I need a quick small answer, how to replace in a string (txt) a character or phrase ("hello") with another character or phrase ("world").
Something like string.replace(txt, "hello", "world")

old Re: String Replace

sixpack
User Off Offline

Quote
For some reason this does not work!
Works fine when I use strings, but it does not work when replacing using a tabled dictionary.

old Re: String Replace

Apache uwu
User Off Offline

Quote
If you have a table you need to loop through all the items in the array.

1
2
3
4
5
6
7
8
replace_from={"lol","cats"}
replace_to=""

str="my cats are pretty awesome and lols"

for _,item in ipairs(replace_from) do
	str=str:gsub(item,replace_to)
end

old Re: String Replace

sixpack
User Off Offline

Quote
I actually have 2 tables with 1:1 match:

table1 = {"a","b"}
table2 = {"1","2"}
a->1
b->2
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview