asp字符串截取问题
a= "我我我我我我我我 "
b= "aaaaaaaaaaaaaaaa "
c= "我a我我我我我我 "
想问下 以上这些情况
我都想只取6字节或5字节
比方说 a 的话 截取后= "我我我 "
b的话 截取后= "aaaaaa "
c的话 如果取6字节可能会出现半个汉字 那么想取 "我a我 "
请大家指教一下
------解决方案--------------------Class execString
'字符串截取处理
Private reg
Private Sub class_Initialize()
'构造函数初始化变量
Set reg = new RegExp '创建正则对象
reg.Pattern = "[\x00-\xff] " '匹配单字节字符
reg.Global = True '全局可用性为是
End Sub
Private Sub class_Terminate()
'类结束时清楚变量
Set reg = Nothing '结束正则对象
End Sub
Private Function g_AL(s)
'获取单字节字符数量
g_AL = reg.Execute(s).Count
End Function
Private Function g_OL(s)
'判断是字符类型
If reg.Test(s) Then
g_OL = 1
Else
g_OL = 2
End If
End Function
Private Function g_CL(s, c)
'判断字符串实际总长度
Dim a, b
a = g_AL(s)
b = Int(a / 2 + 0.5)
g_CL = c - a + b
End Function
Public Function LeftS(s, n)
'截取字符串
Dim c, d
c = Len(s)
If g_CL(s, c) > n Then
Dim l, r, f, i, e
l = Left(s, n)
r = Right(s, c - n)
f = g_Al(l)
While i < f
e = e + 1
i = i + g_OL(Mid(r, e, 1))
Wend
LeftS = l + Left(r, e)
Else
LeftS = s
End If
End Function
End Class
Dim wc
Set wc = new execString
Response.Write wc.LeftS( "aaaaaaaaaaaaaaaa ", 6)
Response.Write wc.LeftS( "aaaaaaaaaaaaaaaa ", 7)
Set wc = Nothing