문제

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