急!用VB编写程序,对输入的字符串进行识别,找出其中大写字母、小写字母、空格、数字及其他字符的个数。

来源:百度知道 编辑:UC知道 时间:2024/06/20 17:59:40

遍历字符串,比较其ASCII,分别用不同的变量

参考代码

Dim intNumber As Integer, intUcase As Integer, intLcase As Integer, intSpace As Integer
Dim i As Integer, strTmp As String

intNumber = 0
intUcase = 0
intLcase = 0
intSpace = 0

For i = 1 To Len(Text1.Text)
strTmp = Mid(Text1.Text, i, 1)
If Asc(strTmp) > 47 And Asc(strTmp) < 58 Then intNumber = intNumber + 1
If Asc(strTmp) > 64 And Asc(strTmp) < 91 Then intUcase = intUcase + 1 '大写字母个数
If Asc(strTmp) > 96 And Asc(strTmp) < 123 Then intLcase = intLcase + 1
If Asc(strTmp) = 32 Then intSpace = intSpace + 1
Next

可以把字符遍历一下,然后用ASCII值来判断统计,具体函数请参考MSDN。

我怎么觉得我前几天回答了这个问题呀!

Option Explicit
'在窗体中添一个CommandButton、一个TextBox
Private Sub Command1_Click()
Dim IntL As Long, IntU As Long, IntS As Long, IntN As Long, IntO As Long
Dim I A