如果数据库某字段的长度为5,那么是可以插入aaaaa,但如果插入的是五个汉字,由于一个汉字等于两个字符,就会出现数据库操作错误,我用len()方法捕捉长度,但aaaaa和五个汉字的长度都是5,有没有什么其他的方法可以捕捉长度啊,使aaaaa的长度为5,而五个汉字的长度为10
(千万不要用什么汉字*2,因为经常是要混合输入的,有时汉字有时字符)
(千万不要用什么汉字*2,因为经常是要混合输入的,有时汉字有时字符)
这个值就是2
Lenb(StrConv("大s", vbFromUnicode))
这个值就是3
现在数据库大多都是Unicode标准了吧。一个汉字一个字母都算一个长度。
难道你用VF??
lenb("本a"),值为4,实际上要为3呀
lenc("汉字a")
结果是3
Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As String) As Long
The first example uses Len to return the number of characters in a string or the number of bytes required to store a variable. The Type...End Type block defining CustomerRecord must be preceded by the keyword Private if it appears in a class module. In a standard module, a Type statement can be Public.Type CustomerRecord ' Define user-defined type.
ID As Integer ' Place this definition in a
Name As String * 10 ' standard module.
Address As String * 30
End TypeDim Customer As CustomerRecord ' Declare variables.
Dim MyInt As Integer, MyCur As Currency
Dim MyString, MyLen
MyString = "Hello World" ' Initialize variable.
MyLen = Len(MyInt) ' Returns 2.
MyLen = Len(Customer) ' Returns 42.
MyLen = Len(MyString) ' Returns 11.
MyLen = Len(MyCur) ' Returns 8.The second example uses LenB and a user-defined function (LenMbcs) to return the number of byte characters in a string if ANSI is used to represent the string.
'------------------------
'下面这个例子反映了len、lenb、LenB(StrConv(str, vbFromUnicode))之间的不同
Function LenMbcs (ByVal str as String)
LenMbcs = LenB(StrConv(str, vbFromUnicode))
End FunctionDim MyString, MyLen
MyString = "ABc"
' Where "A" and "B" are DBCS and "c" is SBCS.
MyLen = Len(MyString)
' Returns 3 - 3 characters in the string.
MyLen = LenB(MyString)
' Returns 6 - 6 bytes used for Unicode.
MyLen = LenMbcs(MyString)
' Returns 5 - 5 bytes used for ANSI.
SDBS:single-byte character set 单字节编码字符
还有一个api能实现一步就到位
function Mylen(str)
if len(str)=0 then
Mylen=0
exit function
end if
dim i
for i=1 to len(str)
if asc(mid(str,i,1))>=0 and asc(mid(str,i,1))<=255 then
Mylen=Mylen+1
else
Mylen=Mylen+2
end if
next
end function
'原理就是:如果是中文或者其他大字符集内的字符的话,
'asc()函数会返回负数!
'如果更深层次的讨论,可以这样说,
'用1位(8bit)最多表示256个字符,即asc码为0-255的字符,
'除此以外的字符都至少要占用你2byte的空间