ex4_3.lua 546 B

123456789101112131415161718
  1. local function insert(str, i, o)
  2. local first_half = str:sub(1, i - 1)
  3. local latter_half = str:sub(i, -1)
  4. return first_half .. o .. latter_half
  5. end
  6. local function tests()
  7. local res = insert("hello world", 1, "start: ")
  8. print(('insert("hello world", 1, "start: ") --> %s'):format(res))
  9. res = insert("hello world", 7, "small ")
  10. print(('insert("hello world", 7, "small ") --> %s'):format(res))
  11. res = insert("hello world", 12, "!")
  12. print(('insert("hello world", 12, "!") --> %s'):format(res))
  13. end
  14. tests()