Modifying a character in a string in Lua

前端 未结 3 1550
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-02 23:17

Is there any way to replace a character at position N in a string in Lua.

This is what I\'ve come up with so far:

function replace_char(pos, str, r)
         


        
3条回答
  •  离开以前
    2021-01-02 23:42

    With luajit, you can use the FFI library to cast the string to a list of unsigned charts:

    local ffi = require 'ffi'
    txt = 'test'
    ptr = ffi.cast('uint8_t*', txt)
    ptr[1] = string.byte('o')
    

提交回复
热议问题