[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Replacing substrings in a string
- From: David Given <dg@...>
- Date: Sun, 04 Sep 2005 01:39:33 +0100
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
One thing I find myself needing to do suprisingly frequently is to replace all
occurances of a particular substring with a different substring, in a larger
string.
Aha, you cry. string.gsub() is exactly what I'm looking for... except it's
not, because I can't turn off regular expression processing, and my pattern is
algorithmically generated and might contain '%' characters.
What's the best way around this? What I'd *like* is for a 'plain' parameter to
be added to string.gsub(), but of course that's unlikely to happen. So far
I've been using a fairly naive approach --- repeatedly calling string.find,
constructing a list of fragments, and then concatenating them with
table.concat. It's a bit verbose and quite irritating, and I'm not convinced
about the speed. Are there any more cunning ways of doing this?
- --
+- David Given --McQ-+ "If you're up against someone more intelligent
| dg@cowlark.com | than you are, do something insane and let him think
| (dg@tao-group.com) | himself to death." --- Pyanfar Chanur
+- www.cowlark.com --+
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
iD8DBQFDGkJBf9E0noFvlzgRAuVpAJ4jiaMLDf6zYF65rD4M4nKI1C8PsgCfZQlr
lkPtb3ONAY3yIRYvF/IiUPg=
=0Ce6
-----END PGP SIGNATURE-----