Dim a() As String = {"YouHaveACat", "SheLovesDogs"}
Dim b(1) As String要处理的字母串是大写字母分隔的,但是单词之间没有空格,怎么样实现以下结果?最好是有现成的函数。 b(0) = "You Have A Cat"
b(1) = "She Loves Dogs"
Dim b(1) As String要处理的字母串是大写字母分隔的,但是单词之间没有空格,怎么样实现以下结果?最好是有现成的函数。 b(0) = "You Have A Cat"
b(1) = "She Loves Dogs"
我是用visual studio 2008
Option Explicit
Dim a As String
Dim strout As String
Dim b() As Long
Dim strSj() As String
Dim i As Long
Dim j As LongPrivate Sub Form_Load()
a = "YouHaveACat"
Call zh(a)
End SubPublic Function zh(a As String)
For i = 1 To Len(a)
If Asc(Mid(a, i, 1)) >= 65 And Asc(Mid(a, i, 1)) <= 90 Then
j = j + 1
End If
Next
ReDim b(1 To j)
ReDim strSj(1 To j)
j = 1
For i = 1 To Len(a)
If Asc(Mid(a, i, 1)) >= 65 And Asc(Mid(a, i, 1)) <= 90 Then
b(j) = i
j = j + 1
End If
Next
For j = LBound(b) To UBound(b) - 1
strSj(j) = Mid(a, b(j), b(j + 1) - b(j))
Next
strSj(UBound(b)) = Mid(a, b(UBound(b)))
For j = LBound(b) To UBound(b)
strout = strout & strSj(j) & " "
Next
Debug.Print strout
End Function
Dim b(1) As String
Dim x As String
Dim I As Integer = 65
Dim J As Integer = 0 For Each x In a
While I >= 65 And I <= 90
If InStr(x, Chr(I)) > 0 Then
x = Replace(x, Chr(I), " " & Chr(I))
End If
I += 1
End While
b(J) = x
J += 1
I = 65
Next
Option ExplicitSub Main()
Dim a() As String
Dim b(1) As String
Dim i As Long
a = Split("YouHaveACat,SheLovesDogs", ",")
For i = 0 To 1
b(i) = AddSpace(a(i))
Debug.Print b(i)
Next
End SubFunction AddSpace(ByVal s As String) As String
Dim iAscii As Long For iAscii = vbKeyA To vbKeyZ
s = Replace(s, Chr(iAscii), " " & Chr(iAscii))
Next AddSpace = Trim$(s)
End Function
Dim s2 As String
Dim ch As String
Dim i As Long
Dim j As Long s2 = Space(Len(s1) * 2)
j = 1
For i = 1 To Len(s1)
ch = Mid$(s1, i, 1)
If ("A" <= ch) And (ch <= "Z") Then
j = j + 1
End If
Mid$(s2, j, 1) = ch
j = j + 1
Next AddSpace = Trim$(s2)
End Function
Inherits System.Windows.Forms.Form
Dim a As String
Dim strout As String
Dim b() As Integer
Dim strSj() As String
Dim i As Integer
Dim j As Integer
Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
a = "YouHaveACat"
Call zh()
Text1.Text = strout
End Sub
Public Sub zh()
For i = 1 To Len(a)
If Asc(Mid(a, i, 1)) >= 65 And Asc(Mid(a, i, 1)) <= 90 Then
j = j + 1
End If
Next
ReDim b(j - 1)
ReDim strSj(j - 1)
j = 0
For i = 1 To Len(a)
If Asc(Mid(a, i, 1)) >= 65 And Asc(Mid(a, i, 1)) <= 90 Then
b(j) = i
j = j + 1
End If
Next
For j = LBound(b) To UBound(b) - 1
strSj(j) = Mid(a, b(j), b(j + 1) - b(j))
Next
strSj(UBound(b)) = Mid(a, b(UBound(b)))
For j = LBound(b) To UBound(b)
strout = strout & strSj(j) & " "
Next
End Sub
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim a() As String = {"YouHaveACat", "SheLovesDogs"}
Dim b() As String, i As Int16
ReDim b(UBound(a))
For i = 0 To UBound(a)
b(i) = iReplace(a(i))
Debug.Print(b(i))
Next
End Sub Private Function iReplace(ByVal a As String) As String
Dim i As Int16
For i = 2 To Len(a)
If Asc(Mid(a, i, 1)) < 97 Then
a = a.Insert(i - 1, " ")
i += 1
End If
Next
iReplace = a
End Function
End Class
Private Sub Form_Load()
MsgBox setSpace("YouHaveACat")
End SubPrivate Function setSpace(strSource$) As String
Dim reg As Object
Set reg = CreateObject("vbscript.regexp")
reg.Global = True
reg.IgnoreCase = False
reg.Pattern = "([A-Z])"
setSpace = Trim(reg.Replace(strSource, " $1"))
Set reg = Nothing
End Function
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MsgBox(setSpace("YouHaveACat"))
End Sub Private Function setSpace(ByVal strSource$) As String
Dim reg As Object
reg = CreateObject("vbscript.regexp")
reg.Global = True
reg.IgnoreCase = False
reg.Pattern = "([A-Z])"
setSpace = Trim(reg.Replace(strSource, " $1"))
reg = Nothing
End Function
End Class
Dim strP As String
Dim strT As String
Dim intP As Integer
strP = UCase(Text1.Text)
strT = Text1.Text
Text2.Text = Mid(strT, 1, 1)
For intP = 2 To Len(strT)
If Mid(strP, intP, 1) = Mid(strT, intP, 1) Then
Text2.Text = Text2.Text & " "
End If
Text2.Text = Text2.Text & Mid(strT, intP, 1)
Next intP
End Sub这段代码只针对你的大写后面添加空格做了处理,至于字符串分段,你可以使用Split函数来实现,很简单。
Dim sBuilder As New StringBuilder(s)
Dim value As Char
Dim i As Integer = 1
Do While sBuilder.Length > i
value = sBuilder.Chars(i)
If value >= "A"c And value <= "Z"c Then
sBuilder.Insert(i, " "c, 1)
i += 1
End If
i += 1
Loop
Return sBuilder.ToString()
End Function Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a() As String = {"YouHaveACat", "SheLovesDogs"}
Dim b(1) As String For i As Integer = 0 To a.Length - 1
b(i) = GetString(a(i))
Console.WriteLine(b(i))
Next
End Sub