Skip to content

Instantly share code, notes, and snippets.

@yunyyyun
Created May 27, 2017 07:45
Show Gist options
  • Save yunyyyun/8bd804be0a1c78f436ca5039baa5d294 to your computer and use it in GitHub Desktop.
Save yunyyyun/8bd804be0a1c78f436ca5039baa5d294 to your computer and use it in GitHub Desktop.
将字符串里的中文(utf-8)转为驼峰格式拼音
------------------------------chinesePhoneticData.lua
local hash = {};
hash = {
["中"] = "Zhong";
["国"] = "Guo";
["股"] = "Gu";
["票"] = "Piao";
["名"] = "Ming";
["称"] = "Cheng";
["代"] = "Dai";
["码"] = "Ma";
}
return hash
------------------------------chinesePhoneticData.lua
local chinesePhoneticData = require ("chinesePhoneticData")
--将字符串里的中文(utf-8)转为驼峰格式拼音,例如:股票代码bryz->guPiaoDaiMabryz
function getHumpNaming(chineseWords)
local f = '[\\0-\\9\65-\90\97-\122\194-\244][\128-\191]*'
local humpNaming = "";
for v in chineseWords:gfind(f) do
if #v == 1 and v ~= nil then
humpNaming = humpNaming .. v
elseif #v == 3 and v~= nil then --常用中文字符用utf-8编码占用3个字节,这里不考虑特殊情况
humpNaming = humpNaming ..(chinesePhoneticData[v] or "[]")
end
end
humpNaming = string.lower(string.sub(humpNaming,1,1))..(string.sub(humpNaming,2,-1) or "")
return humpNaming
end
local testS = "股票代码123abcABC"
print(getHumpNaming(testS))
for i,v in pairs(arg) do
print("----------------------["..v.."]-->"..getHumpNaming(v))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment