查看: 190  |  回复: 0
VB6 获得中文字符个数 子程序f_CountChineseCharacters
楼主
发表于 2023年12月27日 18:03

新建From1(窗体),新建Text1(TextBox),Command1(按钮CommandButton),代码:

Private Function f_CountChineseCharacters(ByVal strText As String) As Long
    Dim i As Long
    Dim lngCount As Long
    
    ' 遍历字符串中的每个字符
    For i = 1 To Len(strText)
        ' 判断当前字符是否为中文字符
        If AscW(Mid$(strText, i, 1)) > 127 Then
            ' 增加计数
            lngCount = lngCount + 1
        End If
    Next i
    
    f_CountChineseCharacters = lngCount
End Function

Private Sub Command1_Click()
    Dim strText As String
    Dim lngCount As Long
    
    Text1.Text = "Hello码农库MaNongKu.com真好"
    
    ' 获取 Text1.Text 中的中文字符个数
    strText = Text1.Text
    lngCount = f_CountChineseCharacters(strText)
    
    MsgBox "中文字符个数:" & lngCount
End Sub

运行结果:

中文字符个数:5


您需要登录后才可以回帖 登录 | 立即注册
【本版规则】请勿发表违反国家法律的内容,否则会被冻结账号和删贴。
用户名: 立即注册
密码:
2020-2024 MaNongKu.com