TestUTF8.cs

  1. using UnityEngine;
  2. using LuaInterface;
  3. public class TestUTF8 : LuaClient
  4. {
  5. string script =
  6. @"
  7. local utf8 = utf8
  8. function Test()
  9. local l1 = utf8.len('你好')
  10. local l2 = utf8.len('こんにちは')
  11. print('chinese string len is: '..l1..' japanese len: '..l2)
  12. local s = '遍历字符串'
  13. for i in utf8.byte_indices(s) do
  14. local next = utf8.next(s, i)
  15. print(s:sub(i, next and next -1))
  16. end
  17. local s1 = '天下风云出我辈'
  18. print('风云 count is: '..utf8.count(s1, '风云'))
  19. s1 = s1:gsub('风云', '風雲')
  20. local function replace(s, i, j, repl_char)
  21. if s:sub(i, j) == '辈' then
  22. return repl_char
  23. end
  24. end
  25. print(utf8.replace(s1, replace, '輩'))
  26. end
  27. ";
  28. protected override LuaFileUtils InitLoader()
  29. {
  30. return new LuaResLoader();
  31. }
  32. //屏蔽,例子不需要运行
  33. protected override void CallMain() { }
  34. protected override void OnLoadFinished()
  35. {
  36. base.OnLoadFinished();
  37. luaState.DoString(script);
  38. LuaFunction func = luaState.GetFunction("Test");
  39. func.Call();
  40. func.Dispose();
  41. func = null;
  42. }
  43. }

?