本文介绍了string.find在Android中无法显示中文字符,但在开发cocos-lua游戏时在PC上成功的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 string.find(中国",中").当我开发我的cocos-lua游戏时,它在PC上成功,但在Android上失败.

I try to use string.find("中国", "中"). It successed on PC but failed on Android when I develop my cocos-lua game.

在Android上, string.find 返回 nil

on Android, string.find return nil

首先,我认为它们的编码可能有所不同,所以我尝试打印出它们的字节.

Fristly, I think their encoding may be diffent, so I try to print out their byte.

在Android上:text1:中国",text2:中".

on Android: text1: "中国", text2:"中".

local text1 = self.__editBox2:getText()
local text2 = self.__editBox3:getText()
local code1 = ""
for i = 1, string.len(text1) do
    code1 = code1 .. "-" .. tostring(string.byte(text1, i))
end

local code2 = ""
for i = 1, string.len(text2) do
    code2 = code2 .. "-" .. tostring(string.byte(text1, i))
end

self.__editBox2:setText(code1)


self.__editBox3:setText(code2)

local a, b = string.find(text1, text2)
local data = tostring(a) .. ":" .. tostring(b)
self.__editBox1:setText(data)

text1:

228-184-173-229-155-189

text2:

228-184-173

答案仍然是:

nil:nil

PS: string.find

static int str_find_aux (lua_State *L, int find) {
  size_t l1, l2;
  const char *s = luaL_checklstring(L, 1, &l1);
  const char *p = luaL_checklstring(L, 2, &l2);
  ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1;
  if (init < 0) init = 0;
  else if ((size_t)(init) > l1) init = (ptrdiff_t)l1;
  if (find && (lua_toboolean(L, 4) ||  /* explicit request? */
      strpbrk(p, SPECIALS) == NULL)) {  /* or no special characters? */
    /* do a plain search */
    const char *s2 = lmemfind(s+init, l1-init, p, l2);
    if (s2) {
      lua_pushinteger(L, s2-s+1);
      lua_pushinteger(L, s2-s+l2);
      return 2;
    }
  }
  else {
    MatchState ms;
    int anchor = (*p == '^') ? (p++, 1) : 0;
    const char *s1=s+init;
    ms.L = L;
    ms.src_init = s;
    ms.src_end = s+l1;
    do {
      const char *res;
      ms.level = 0;
      if ((res=match(&ms, s1, p)) != NULL) {
        if (find) {
          lua_pushinteger(L, s1-s+1);  /* start */
          lua_pushinteger(L, res-s);   /* end */
          return push_captures(&ms, NULL, 0) + 2;
        }
        else
          return push_captures(&ms, s1, res);
      }
    } while (s1++ < ms.src_end && !anchor);
  }
  lua_pushnil(L);  /* not found */
  return 1;
}


static int str_find (lua_State *L) {
  return str_find_aux(L, 1);
}

推荐答案

Lua没有对开箱即用的unicode字符的适当支持,但是有很好的库可以解决这个问题.我从来没有使用过cocos2d,我不确定他们是否有任何附加组件可以解决这个问题.但是您可以尝试使用以下一种: https://luarocks.org/modules/xavier-wang/luautf8.我曾经成功使用过一次.希望这会有所帮助!

Lua does not have proper support to unicode characters out of the box, but there are good libraries that will fix that. I have never used cocos2d and I'm unsure if they have any add-ons to deal with this. But you could try using this one:https://luarocks.org/modules/xavier-wang/luautf8. I have used it with success once. Hope this helps!

这篇关于string.find在Android中无法显示中文字符,但在开发cocos-lua游戏时在PC上成功的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 21:50