如文本内容为“123测试”,如果现在选中“试”,selStart为4,其单字节的位置为5。请教是否有VB函数可以使用?谢谢!

解决方案 »

  1.   

    Private Function strlen(str As String) As Long '求字符串的单字节个数
        Dim num As Long
        Dim i As Long
        strlen = 0
        If Trim(str) = "" Then Exit Function
        num = Len(Trim(str))
        For i = 1 To num
            If Asc(Mid(str, i, 1)) < 0 Then
                strlen = strlen + 2
            Else
                strlen = strlen + 1
            End If
        Next
    End FunctionPrivate Sub Command1_Click() '实际调用
        Dim a As String
        a = Left(Text1.Text, Text1.SelStart) '取得光标前的字符串
        MsgBox "光标前的单字节个数=" & strlen(a)
    End Sub
      

  2.   

    lenB用于双字节字符集(DBCS)中,即把字母和汉字都用双字节来表示