Jump to content

Funçao String.gsub(S, Str, Repl, N)


Recommended Posts

Ela funciona como a funçao string.gsub so que nao funciona com patterns...

 

function stringsub(s, str, repl, n)
   n = n ~= nil and n or "inf"
   lastpos = 1

   for i = 1, #s do
       if s:sub(i, i+#str-1) == str then
           if n == "inf" then
               s = s:sub(lastpos, i-1) .. repl .. s:sub(i+#str)
               a = a ~= nil and a+1 or 1
           else
               if a == nil or a < n then
                   s = s:sub(lastpos, i-1) .. repl .. s:sub(i+#str)
                   a = a ~= nil and a+1 or 1
               else
                   break
               end
           end
       end
   end
return s , a
end

 

Eu sei que dava pra fazer usando string.find facilmente. Mas nao teria graça ne? xD

Edited by MatheusMkalo
Link to comment
Share on other sites

  • 5 weeks later...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...