How can I set a character in a string to something else by knowing its position?

Stupid question, but how can I set a character in a string to something else, knowing the position of the character that I want to change ?

Is there a built in function for this ? I couldn’t find anything on google. Please help.

1 Like
string.gsub(String, "%CHAR", REPLACEMENTCHAR)

I don’t see why getting the position of the char in a string would be beneficial for this. I could be missing something.

I misworded the original post, I mean to say what function is used to change the character at a position in the string.

Would gsub not work for this?

If I understand correctly you could do something like this.

function replaceChar(toReplace, position, replaceWith)
	return string.sub(toReplace, 1, position-1)..replaceWith..string.sub(toReplace, position+1);
end

print(replaceChar("Hello warld!", 8, "o"));
2 Likes