VB字符统计函数
在“通用声明”代码区声明(窗体)模块级变量
Option Explicit
Dim letters As Integer '声明模块级变量,此变量计算字母个数
Dim space As Integer '空格个数
Dim digit As Integer '数字个数
Dim others As Integer '其他字符个数
输入cmdStat_Click()事件过程代码
Private Sub cmdStat_Click()
Dim InputStr As String '局部变量,此变量存储输入的字符串
Dim i As Integer '循环控制变量,整型
Dim CaseStr As String '此变量保存储所截取的字符
letters = 0 '初始化为0
space = 0
digit = 0
others = 0
InputStr = txtInput.Text '取得输入的字符串
For i = 1 To Len(InputStr) '开始分别统计个数
CaseStr = Mid(InputStr, i, 1) '取得某个字符
Select Case CaseStr
Case "a" To "z", "A" To "Z" '如果字符是英文字母
letters = letters + 1
Case " " '如果字符是空格
space = space + 1
Case 0 To 9 '如果字符是数字
digit = digit + 1
Case Else '如果字符是其他字母
others = others + 1
End Select
Next
'以下代码用来显示统计出的结果值
txtResult1.Text = letters
txtResult2.Text = space
txtResult3.Text = digit
txtResult4.Text = others
txtResult5.Text = Len(InputStr)
End Sub
Option Explicit
Dim letters As Integer '声明模块级变量,此变量计算字母个数
Dim space As Integer '空格个数
Dim digit As Integer '数字个数
Dim others As Integer '其他字符个数
输入cmdStat_Click()事件过程代码
Private Sub cmdStat_Click()
Dim InputStr As String '局部变量,此变量存储输入的字符串
Dim i As Integer '循环控制变量,整型
Dim CaseStr As String '此变量保存储所截取的字符
letters = 0 '初始化为0
space = 0
digit = 0
others = 0
InputStr = txtInput.Text '取得输入的字符串
For i = 1 To Len(InputStr) '开始分别统计个数
CaseStr = Mid(InputStr, i, 1) '取得某个字符
Select Case CaseStr
Case "a" To "z", "A" To "Z" '如果字符是英文字母
letters = letters + 1
Case " " '如果字符是空格
space = space + 1
Case 0 To 9 '如果字符是数字
digit = digit + 1
Case Else '如果字符是其他字母
others = others + 1
End Select
Next
'以下代码用来显示统计出的结果值
txtResult1.Text = letters
txtResult2.Text = space
txtResult3.Text = digit
txtResult4.Text = others
txtResult5.Text = Len(InputStr)
End Sub