如果数据库某字段的长度为5,那么是可以插入aaaaa,但如果插入的是五个汉字,由于一个汉字等于两个字符,就会出现数据库操作错误,我用len()方法捕捉长度,但aaaaa和五个汉字的长度都是5,有没有什么其他的方法可以捕捉长度啊,使aaaaa的长度为5,而五个汉字的长度为10
(千万不要用什么汉字*2,因为经常是要混合输入的,有时汉字有时字符)

解决方案 »

  1.   

    Lenb(StrConv("大", vbFromUnicode))
    这个值就是2
    Lenb(StrConv("大s", vbFromUnicode))
    这个值就是3
      

  2.   

    或者用API的LSTRLEN取得实际长度
      

  3.   

    你有什么数据库啊?
    现在数据库大多都是Unicode标准了吧。一个汉字一个字母都算一个长度。
    难道你用VF??
      

  4.   

    lenb()?
    lenb("本a"),值为4,实际上要为3呀
      

  5.   

    这样解决
    lenc("汉字a")
    结果是3
      

  6.   

    可以借助Windows API的函数lstrlen
    Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As String) As Long
      

  7.   

    msdn中关于lenb、len的例子:
    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.
      

  8.   

    DBCS:double-byte character set  双字节编码字符
    SDBS:single-byte character set  单字节编码字符
      

  9.   

    http://www.somade.com/是个很专业的技术社区,去那里找找吧,或许有你要的答案~
      

  10.   

    用StrConv函数
    还有一个api能实现一步就到位
      

  11.   

    怎么知道这个字符是DBCS和SBCS呢?
      

  12.   

    原来做网页程序的时候也经常会碰到这个问题,看看我当时是怎样整的吧,我用的没什么问题:
    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的空间
      

  13.   

    建議使用API函數,返回的字節數,比較快,不需要過多的轉換.Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As String) As Long