VB程序编写,统计从文本框输入的字符串中的字母数字空格和其他字符的个数由不同文本框显示个数
发布网友
发布时间:2022-05-20 19:12
我来回答
共2个回答
热心网友
时间:2023-11-15 09:23
Private Sub Command1_Click()
Dim str As String, strTmp As String, intN As Integer, intM As Integer
Dim intX As Integer, intY As Integer, intZ As Integer, intW As Integer
str = Text1.Text
intN = Len(str)
For intM = 1 To intN
strTmp = UCase(Mid(str, intM, 1))
If IsNumeric(strTmp) Then '数字
intX = intX + 1
ElseIf Asc(strTmp) > 64 And Asc(strTmp) < 91 Then '字母
intY = intY + 1
ElseIf strTmp = " " Then '空格
intZ = intZ + 1
Else '其它
intW = intW + 1
End If
Next
Text2.Text = "数字个数为:" & intX
Text3.Text = "字母个数为:" & intY
Text4.Text = "空格个数为:" & intZ
Text5.Text = "特殊字符个数为:" & intW
End Sub
'如果满意我的回答,请采纳.谢谢
热心网友
时间:2023-11-15 09:23
'写错了 我改了下
function stABC(word)'字母个数
Abc="abcdefghijklmnopqrstuvwxyz" '所有的字母都是小写的啊
word=lcase(word)'小写所有的字母我自己用小写的来判断的
for i=1 to len(word)
if instr(abc,mid(word,i,1))>0 then '找到了当然就+1了
t=t+1
end if
next
stABC=t '返回找到的字母数
end function
'统计数字
function stNo(word)
no="0123456789" '
for i=1 to len(word)
if instr(no,mid(word,i,1))>0 then '找到了当然就+1了
t=t+1
end if
next
stNo=t '返回找到的数
end function
'统计空格
function stSp(word)
l1=len(word)
l2=len(replace(word," ",""))'把空格全替换了再求长
stSp=l1-l2 '-下就是我们空格数了
end function
function lenNo(word)
lenNo=len(word)
end function
'下面开始使用了
tword=inputbox("输入要统计的字符吧!")
showWord="字母个数为:"&stabc(tword)
showword=showword+"数字个数为:" &stno(tword)
showword=showword+"空格个数为:" &stsp(tword)
showword=showword+"长度为:" &lenno(tword)
msgbox(showword)