Dim a() As String = {"YouHaveACat", "SheLovesDogs"}
        Dim b(1) As String要处理的字母串是大写字母分隔的,但是单词之间没有空格,怎么样实现以下结果?最好是有现成的函数。        b(0) = "You Have A Cat"
        b(1) = "She Loves Dogs"

解决方案 »

  1.   

    LZ:你如此定义的变量似乎是VB.NET的写法.
      

  2.   


    我是用visual studio 2008
      

  3.   

    给段VB6调试通过的代码:
    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
      

  4.   

    LZ:VB6代码用到VB.NET要化费一番脑筋的.
      

  5.   

            Dim a() As String = {"YouHaveACat", "SheLovesDogs"}
            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
      

  6.   

    给个效率不算高但是很容易理解的方法(VB6)
    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
      

  7.   

    效率高一点用这个Function AddSpace(ByVal s1 As String) As String
        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
      

  8.   

    转换为VB.NET2008的代码:
    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
      

  9.   


    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
      

  10.   

    为什么没人用正则呢,我觉得每个人都应该学学正则,效率其实也不低的。
    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
      

  11.   

    vb2005
    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
      

  12.   

    Option ExplicitPrivate Sub Command1_Click()
        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函数来实现,很简单。
      

  13.   

    正则比较简单,不过.net高效的方法感觉是StringBuilder类    Private Function GetString(ByVal s As String) As String
            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
      

  14.   

    net中有正则类,不用CreateObject("vbscript.regexp"),这个功能太有限了...