Using string.gsub multiple times?
Started by gknova61, Mar 07 2013 04:31 PM
8 replies to this topic
#1
Posted 07 March 2013 - 04:31 PM
Say if you're in the following situation
You have something like asdfghj let's declare this as a variable
string = asdafghsja
and you need to turn the a's in there to s's and the s's to a's, you'd use string.gsub right like this?
string.gsub(string,"a","s")
string.gsub(string,"s","a")
I want to turn the a's into s and the s's into a without it conflicting and turning all the a's into s's then all the s's it just changed into all a's. Basically I want this final product:
sadsfghajs
Instead of the following which is what the above code would yield:
aadafghaja
In case it's still not clear, like a way to use gsub but to replace multiple things at the exact same time and not in order so they don't overwrite eachother.
You have something like asdfghj let's declare this as a variable
string = asdafghsja
and you need to turn the a's in there to s's and the s's to a's, you'd use string.gsub right like this?
string.gsub(string,"a","s")
string.gsub(string,"s","a")
I want to turn the a's into s and the s's into a without it conflicting and turning all the a's into s's then all the s's it just changed into all a's. Basically I want this final product:
sadsfghajs
Instead of the following which is what the above code would yield:
aadafghaja
In case it's still not clear, like a way to use gsub but to replace multiple things at the exact same time and not in order so they don't overwrite eachother.
#2
Posted 07 March 2013 - 04:38 PM
I've never personally needed to use string.gsub like you need to use it. If this doesn't help then i'm sure someone else would be able to solve your problem.
EDIT: I am guessing you will need to get where all the a's are at and pull them from the string, then turn them into s's and insert them back into the string with the position they were in before you pulled them. Do the same with the s's, but pull them both at the same time. (If that makes sense) You can get the location of a char in the string by using string.find() which is in the link I gave you
EDIT: I am guessing you will need to get where all the a's are at and pull them from the string, then turn them into s's and insert them back into the string with the position they were in before you pulled them. Do the same with the s's, but pull them both at the same time. (If that makes sense) You can get the location of a char in the string by using string.find() which is in the link I gave you
#3
Posted 07 March 2013 - 05:24 PM
Just change all the other s's before changing the a's to s's to - or something odd.
s = 'asdafghsja' -- if u name it string, it will override the string api! string.gsub(s, 's', '~' string.gsub(s,"a","s") string.gsub(s,"s","a") string.gsub(s, '~', 's') print(s) -- should be asdafghsja
#5
#6
Posted 07 March 2013 - 06:06 PM
You can do this.
It finds every match of an a or an s, and replaces it with an a if it's an s, and vice versa.
s = string.gsub('asdafghsja','[as]',function(letter)
return letter == 'a' and 's' or 'a'
end)
It finds every match of an a or an s, and replaces it with an a if it's an s, and vice versa.
#7
Posted 07 March 2013 - 06:34 PM
immibis, on 07 March 2013 - 06:03 PM, said:
Yeah, kinda figured. Why would you even want to name a table "table or a string "string"? Pretty useless names. Although I see it sometimes in Ask A Pro... People really need to read the Lua Reference Manual. Cloudy should link it in the confirmation email you get when you join
#8
Posted 07 March 2013 - 07:42 PM
SuicidalSTDz, on 07 March 2013 - 06:34 PM, said:
immibis, on 07 March 2013 - 06:03 PM, said:
If you name any global "table" it will override the table API.
#9
Posted 07 March 2013 - 08:01 PM
immibis, on 07 March 2013 - 07:42 PM, said:
You can actually overwrite string with a string without breaking its use as an API. Strings have a hidden metatable with an __index field pointing to a table with all the string functions - that's how myString:sub etc can work.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











