StringUtil.lua
4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
cc.exports.StringUtil = {}
function StringUtil.split(str,split_char)
str = tostring(str);
local sub_str_tab = {};
while (true) do
local pos = nil
for i = 1, #str do
if string.sub(str, i, i) == split_char then
pos = i
break
end
end
if (not pos) then
sub_str_tab[#sub_str_tab + 1] = str;
break;
end
local sub_str = string.sub(str, 1, pos - 1);
sub_str_tab[#sub_str_tab + 1] = sub_str;
str = string.sub(str, pos + 1, #str);
end
return sub_str_tab;
end
function GetPreciseDecimal(nNum, n)
if type(nNum) ~= "number" then
return nNum;
end
n = n or 0;
n = math.floor(n)
local fmt = '%.' .. n .. 'f'
local nRet = tonumber(string.format(fmt, nNum))
return nRet;
end
--获取数字显示万
function StringUtil.getWanString(value)
local str = "";
if value >= 100000000 then
local w = tostring(math.floor(value/100000000));
local num = 4-string.len(w);
if num < 0 then
num = 0;
end
print("亿---------------------:",value/100000000,num);
str = GetPreciseDecimal(value/100000000,num).."亿";
elseif value >= 10000 then
local w = tostring(math.floor(value/10000));
local num = 4-string.len(w);
if num < 0 then
num = 0;
end
str = GetPreciseDecimal(value/10000,num).."万";
else
str = tostring(value);
end
return str;
end
--将每个字符分离出来,放到table中,一个单元内一个字符
local function StringToTable(s)
local tb = {};
--[[
UTF8的编码规则:
1. 字符的第一个字节范围: 0x00—0x7F(0-127),或者 0xC2—0xF4(194-244); UTF8 是兼容 ascii 的,所以 0~127 就和 ascii 完全一致
2. 0xC0, 0xC1,0xF5—0xFF(192, 193 和 245-255)不会出现在UTF8编码中
3. 0x80—0xBF(128-191)只会出现在第二个及随后的编码中(针对多字节编码,如汉字)
]]
for utfChar in string.gmatch(s, "[%z\1-\127\194-\244][\128-\191]*") do
table.insert(tb, utfChar);
end
return tb;
end
--获取字符串长度,设一个中文长度为2,其他长度为1
function StringUtil.GetUTFLen(s)
local sTable = StringToTable(s);
local len = 0;
local charLen = 0;
for i=1,#sTable do
local utfCharLen = string.len(sTable[i])
if utfCharLen > 1 then -- 长度大于1的就认为是中文
charLen = 2;
else
charLen = 1;
end
len = len + charLen;
end
return len;
end
--获取指定字符个数的字符串的实际长度,设一个中文长度为2,其他长度为1,count:-1表示不限制
local function GetUTFLenWithCount(s, count)
local sTable = StringToTable(s);
local len = 0;
local charLen = 0;
local isLimited = (count >= 0);
for i=1,#sTable do
local utfCharLen = string.len(sTable[i]);
if utfCharLen > 1 then -- 长度大于1的就认为是中文
charLen = 2;
else
charLen = 1;
end
len = len + utfCharLen;
if isLimited then
count = count - charLen
if count <= 0 then
break;
end
end
end
return len;
end
--截取指定字符个数的字符串
function StringUtil.getMaxLenString(s, maxLen)
local len = StringUtil.GetUTFLen(s)
local dstString = s;
-- 超长,裁剪,加...
if len > maxLen then
dstString = string.sub(s, 1, GetUTFLenWithCount(s, maxLen));
-- dstString = dstString.."...";
end
return dstString;
end
--在前面补零,转换为固定长度的字符串
function StringUtil.toStringFixedZeroPrefix(value,cnt)
local str = tostring(value);
local len = string.len(str);
local num = cnt - len;
if num > 0 then
local tmp_str = str;
str = "";
for i=1,num do
str = str.."0";
end
str = str..tmp_str;
end
return str;
end
function StringUtil.urlEncode(s)
s = string.gsub(s, "([^%w%.%- ])", function(c) return string.format("%%%02X", string.byte(c)) end)
return string.gsub(s, " ", "+")
end
function StringUtil.urlDecode(s)
s = string.gsub(s, '%%(%x%x)', function(h) return string.char(tonumber(h, 16)) end)
return s
end
return StringUtil