Jump to content




Remove a string between strings?

lua

  • You cannot reply to this topic
6 replies to this topic

#1 Left4Cake

  • Members
  • 272 posts
  • LocationEarth

Posted 18 February 2013 - 10:24 AM

In lua, how would I remove a string that is between two other strings?

Spoiler

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


#2 Shrooblord

  • Members
  • 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

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

#3 JokerRH

  • Members
  • 147 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...:)

#4 NDFJay

  • Members
  • 216 posts
  • LocationLeeds, 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

#5 remiX

  • Members
  • 2,076 posts
  • LocationSouth 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?

#6 Kingdaro

    The Doctor

  • Members
  • 1,636 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.

#7 Left4Cake

  • Members
  • 272 posts
  • LocationEarth

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. :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users