My string is 'Hllo'. I want to put inside it 'e' after the 'H' by its position, this case, position number 2.

有帮助吗?

解决方案 2

You can simply cut contents until position you want to place your character on, then add the character and finally concat the characters on and after position.

src = "Hllo"
result = string.sub(src, 1, string.find(src, "H")) .. "e" .. string.sub(src, string.find(src, "H")+1)

The first part of code gets position of 'H' andf cuts the start (in this case 'H' only). Second part adds character you want to insert. Third part adds every character after 'H' in source string to result.

其他提示

local str = 'Hllo'
str = str:gsub('()',{[2]='e'})

you can try this out

$arr = str_split('hllo',1);
$result=$arr[0].'e'.$arr[1].$arr[2].$arr[3]

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top