比如
str="中国和美国"要截取前面8个字节,变成“中国和美”
str="中国and美国"要截取前面8个字节,变成“中国and”不能出现乱码

解决方案 »

  1.   

    先确定一下,如果str="中and美国",那么你想得到什么样的结果?
      

  2.   

    //str="中国and美国"要截取前面8个字节,变成“中国and”不能出现乱码“中国and”是8字节么?
      

  3.   

    str=  "中and美国  ",应该是中and美
    换句话说如果第8个字节是汉字就不截取或者加一个空格,但是不能发生截取半个汉字的情况
      

  4.   

    private function myleft(byval str as string,byval n as integer) as string 
        while len(str)>0 and n>0
              if lenb(left(str,1))=1 then 
                     myleft=myleft & left(str,1)
                     n=n-1
                     str=right(str,len(str)-1)
              else
                     if n>1 then 
                           myleft=myleft & left(str,1)
                           n=n-2
                           str=right(str,len(str)-1)
                     else
                           n=0
                     end if
              end if
        end while
    end function
      

  5.   

    ? leftb("中国and美国",10)="中国an"? leftb("中国和美国",8)="中国和美"符合楼主要求!
      

  6.   

    ? leftb("中国and美国",10)="中国and"? leftb("中国和美国",8)="中国和美"符合楼主要求!
      

  7.   

    判断你截取的最后一个字符的ASCII码,就可以了啊。
      

  8.   

    看下文章,不知道有没有帮助http://xia0xia0520.blog.163.com/blog/static/9878552006101795849366/
      

  9.   

    也就是leftb("中国and美国",8)="中国an
      

  10.   

    Dim b As String
    Dim c As String
    b = StrConv("中国and美国", vbFromUnicode)
    c = StrConv(LeftB(b, 8), vbUnicode)
    If Len(c) > 0 Then
      If Asc(Right(c, 1)) = 0 Then c = Left(c, Len(c) - 1)
      Debug.Print c
    End Ifb = StrConv("中国和美国", vbFromUnicode)
    c = StrConv(LeftB(b, 8), vbUnicode)
    If Len(c) > 0 Then
      If Asc(Right(c, 1)) = 0 Then c = Left(c, Len(c) - 1)
      Debug.Print c
    End If