修改VB程序:首字母大写

来源:百度知道 编辑:UC知道 时间:2024/05/29 12:13:07
输入一段英文语句,使每个单词的第一个字母变成大写。考虑到空格、各种符号。下面程序有点问题,望高手修改:

Private Sub Command1_Click()
Dim a, a1, s1, s2, b()
f = Array(" ",",", "?", ".", ":", ";", "!")
a = Text1
For j = 0 To UBound(f)
a1 = Split(a, f(j))
ReDim b(UBound(a1))
For i = 0 To UBound(a1)
s1 = Left(a1(i), 1)
s1 = UCase(s1)
s2 = Right(a1(i), Len(a1(i)) - 1)
b(i) = s1 & s2
Next i
For i = 0 To UBound(a1)
txt = txt & b(i) & f(j)
Next i
a = Left(txt, Len(txt) - 1)
txt = ""
Next j
Text1 = a
End Sub
Private Sub Form_Load()
Text1 = "sad,aaa?bd!fds;dg sdf:sdf"
End Sub

一般的能实现,但碰到有两个空格、句未有符号等情况就出错。

'这样写应该没有问题的
Private Sub Command1_Click()
Dim a, b, i, c, d, temp

a = Text1
b = Len(Text1)
d = 1

For i = 1 To b
c = Mid(a, i, 1)

If ((Asc(c) > 96 And Asc(c) < 123) Or (Asc(c) > 64 And Asc(c) < 91)) Then
If d = 1 Then
temp = temp & UCase(c)
d = 0
Else
temp = temp & c
End If
Else
temp = temp & c
d = 1
End If
Next

Text1 = temp
End Sub

Private Sub Form_Load()
Text1 = " sad , aaa ? bd! fds; dg sdf:zdf "
End Sub

修改不大容易,干脆给你写一个吧。建一个Text1,Command1。
代码如下。
==================
Private Sub Command1_Click()
Const ascla = 97 'Asc("a")
Const asclz = 122 'Asc("z")
Const ascuA = 65 'Asc("A")
Const ascuZ = 90 'Asc("Z")
Dim i As Long, t