Navigation menu

WoW:API gsub: Difference between revisions

Jump to navigation Jump to search
35 bytes added ,  25 March 2010
+ strreplace
(Added link)
(+ strreplace)
Line 1: Line 1:
{{:Lua/Libshortcut|gsub|string.gsub}}
{{luaapi}}
  string.gsub(s, pattern, replace [, n])
  string.gsub(s, pattern, replace [, n])
gsub(s, pattern, replace [, n])
strreplace(s, pattern, replace [, n])


This is a very powerful function and can be used in multiple ways. Used simply it can replace all instances of the pattern provided with the replacement. A pair of values is returned, the modified string and the number of substitutions made. The optional fourth argument n can be used to limit the number of substitutions made:
This is a very powerful function and can be used in multiple ways. Used simply it can replace all instances of the pattern provided with the replacement. A pair of values is returned, the modified string and the number of substitutions made. The optional fourth argument n can be used to limit the number of substitutions made:
Line 31: Line 33:
  > = string.gsub("banana", "(a)(n)", function(a,b) return b..a end) -- reverse any "an"s
  > = string.gsub("banana", "(a)(n)", function(a,b) return b..a end) -- reverse any "an"s
  bnanaa  2
  bnanaa  2
{{LUA}}