这是我前几天在工作中用到的一个小程序,此程序只是取出用户输入的汉字数。
希望大家多多提意见。
Private Sub Command1_Click()
Dim Hanzi As String
Dim HanziCount As Integer
Dim hanziNum As Integer
Dim StrFormat
Dim tszf ’输入字符中的全角特殊字符
StrFormat = "!@#¥%……&×()——+~=·;[‘:“” ’。、?《》," ‘ascii码中小于零中的全角特殊字符
Hanzi = Text1.Text ‘到得输入的字符
If Hanzi = "" Then
MsgBox "测试期间请输入内容!", vbInformation + vbOKOnly, "错误提示"
Else
For i = 1 To Len(Hanzi)
If Asc(Mid(Hanzi, i, 1)) < 0 Then
num = num + 1
For j = 1 To Len(StrFormat)
If InStr(Mid(Hanzi, i, 1), Mid(StrFormat, j, 1)) > 0 Then
tszf = tszf & Mid(Hanzi, i, 1)
End If
Next
End If
Next
hanziNum = num - Len(tszf)
MsgBox hanziNum
End If
End Sub
希望大家多多提意见。
Private Sub Command1_Click()
Dim Hanzi As String
Dim HanziCount As Integer
Dim hanziNum As Integer
Dim StrFormat
Dim tszf ’输入字符中的全角特殊字符
StrFormat = "!@#¥%……&×()——+~=·;[‘:“” ’。、?《》," ‘ascii码中小于零中的全角特殊字符
Hanzi = Text1.Text ‘到得输入的字符
If Hanzi = "" Then
MsgBox "测试期间请输入内容!", vbInformation + vbOKOnly, "错误提示"
Else
For i = 1 To Len(Hanzi)
If Asc(Mid(Hanzi, i, 1)) < 0 Then
num = num + 1
For j = 1 To Len(StrFormat)
If InStr(Mid(Hanzi, i, 1), Mid(StrFormat, j, 1)) > 0 Then
tszf = tszf & Mid(Hanzi, i, 1)
End If
Next
End If
Next
hanziNum = num - Len(tszf)
MsgBox hanziNum
End If
End Sub
Text1.Text = StringDoubleWordCount("KiteGirl是个好孩子!")
End SubFunction StringDoubleWordCount(ByVal pString As String, Optional ByVal pCheck As Boolean = False) As Long
'pString 输入字符串
'pCheck 真值掩码。为True时计算字符串中半角的数量,为False时计算字符串中全角的数量。默认为False
Dim tOutLong As Long '输出变量
Dim tBytes() As Byte '输入数组
Dim tBytes_Length As Byte '数组记数容量。
tBytes() = pString '字符串和字节数组可以直接赋值。
tBytes_Length = UBound(tBytes())
Dim tIndex As Long '循环索引
For tIndex = 1 To tBytes_Length Step 2
'注意:由于是高位在后,低位在前(这是数据在内存和文件中存储的基本规律),所以从1开始。否则,是从0开始。
tOutLong = tOutLong + ((CBool(tBytes(tIndex)) Xor pCheck) And 1) '最关键的一句!
Next
StringDoubleWordCount = tOutLong
End Function