lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


[Excuse the top-post, forgot to pack a mouse in my laptop bag.)

The pattern () captures a position and gsub allows a user-specified
function as replacement.

You will also need to capture the characters themselves.

pat = '()(.)'
repl = function(k,c)
  if k>=2 and k<=4 then return ('AUSTRALIA'):sub(k+2,k+2)
  else return c
  end
end
('INDIA'):gsub(pat,repl)


2017-03-11 7:42 GMT+02:00 Naushad Mujawar <naushadmujawar300@gmail.com>:
> Dear Team,
>
> I am beginner in LUA Programming.
> I wanted to write a LUA program to replace the some characters of the string
> by using the index places of those characters.
>
> e.g. If String is "INDIA", then I wanted to replace the characters of the
> string at indexes 2, 3 & 4 with the characters with indexes 4,5 & 6 of other
> string " AUSTRALIA" so that my Output String will be " ITRAA".
>
>  But I am not able to find any standard LUA Function to replace the
> characters of the string by using character indexes.
>
> Please provide me with  any reference guide or any standard lua function to
> replace the strings in lua.
>
>
> Thanks and Regards,
> Naushad Mujawar