280 posts
Location
Earth
Posted 18 February 2013 - 10:24 AM
In lua, how would I remove a string that is between two other strings?
Spoiler
example:
from
<!– remove this –>
to:
<!– –>
Also (looking for separate answer) How would I remove the other strings along with it. Assuming it can be done in the same action as above, otherwise I know how I could do it.
Spoiler
example
from:
Test this out <!– remove this –> bro.
to:
Test this out bro.
note:with out having to include "remove this" in the search.
24 posts
Posted 18 February 2013 - 11:03 AM
If you know exactly how long the string is in its entirety and how long the part is you want to cut out, in theory you should be able to do something like this (I would have to test it out myself - but go ahead and try if you want):
Spoiler
local oldstr = ""
local newstr = ""
local totalstrlength = 0
local strcutoutbegin = 0
local strcutoutend = 0
function replaceStr(oldstr,totalstrlength,strcutoutbegin,strcutoutend)
local tempstr1 = ""
local tempstr2 = ""
--The strsub(s,i,j) function uses string 's' and copies it over starting
--from the character at position i up to the character at position j.
tempstr1 = strsub(oldstr,1,strcutoutbegin-1)
tempstr2 = strsub(oldstr,strcutoutend+1,totalstrlength)
newstr = tempstr1..tempstr2
print(newstr)
return true
end
--[[Call the function]]--
-- Say you want to remove ', defunct' from the following line:
oldstr = "This is the old, defunct string"
--The length of this particular string is 31 characters.
totalstrlength = #oldstr
--From which character in the string do we need to start cutting?
--And where do we need to stop?
--In this case, we want to remove the text starting from the
--16th character up to and including the 24th character.
--16
strcutoutbegin = tonumber(read())
--24
strcutoutend = tonumber(read())
replaceStr(oldstr,totalstrlength,strcutoutbegin,strcutoutend)
--It should now print "This is the old string"
I haven't tried any of this, but I'm basing my code on what lua should be capable of. I don't know if CC uses the full range of lua commands or a select few. If it doesn't have the functions I use, then you'll have to find some other way to do this.
You should also check
http://www.lua.org/m...2.4/node22.html
148 posts
Posted 18 February 2013 - 11:05 AM
You could simply use
string.sub(String, start, stop)
Or you use patterns with string.gmatch (or was it string.match, i don't know…:D/>/>)
Just look at the lua manual:)
Edit: Wow, there was someone faster than me and replied while I was typing…:)/>
212 posts
Location
Leeds, England
Posted 18 February 2013 - 11:11 AM
As JokerRH said, string.sub or string.gmatch are you best options, they're quick and easy to implement and they are what I use along with string.len for my config files in my programs
2088 posts
Location
South Africa
Posted 18 February 2013 - 06:08 PM
You could maybe us string.gsub?
s = "<!-- remove this -->"
s = s:gsub("remove this", "")
print(s)
-- Output: <!-- -->
Are you trying to get information out of an html page?
1688 posts
Location
'MURICA
Posted 18 February 2013 - 06:30 PM
For your purpose, I think this'd work a lot well for more strings than just "remove this".
local s = '<!-- some other words -->'
s = s:gsub('<!%-%-(.-)%-%->',' ')
This removes anything between any set of <!– –> and replaces it with a single space.
280 posts
Location
Earth
Posted 19 February 2013 - 04:09 AM
Thank you all. While Kingdaro and remiX did have the exact answer I was looking for. Everything here helped in someway. :)/>