用字符串拆分字符串?

时间:2014-05-20 20:40:04

标签: vb6 split

如果我有一个字符串:

Dim someStr As String
someStr = "NODE TEST              ACCESS"

我想将该字符串拆分两个空格。看起来Split()函数接受一个字符而不是一个完整的字符串来分割什么。

用字符串分割字符串最简单的方法是什么(在这种情况下是两个空格)?拆分两个或多个空格是好的。我不打算分成两个。

6 个答案:

答案 0 :(得分:2)

好的,在对OP的要求做了一些澄清之后,我们需要稍微增加lardymonkey的想法。所以:

Dim someStr As String
Dim someArray() as String
Dim cLen As Integer 'Keep a count of the current characters in the string

someStr = "NODE TEST              ACCESS"

'Strip off one space at a time from groups of >2 spaces
Do
    cLen = Len(someStr)
    someStr = Replace(someStr, Space$(3), Space$(2)) 
Loop Until cLen = Len(someStr) 'No more replacements were made in the current iteration

'Now you can use lardymonkey's idea
someArray = Split(Replace$(someStr,"  ","|"),"|")

答案 1 :(得分:2)

如果我正确读取OP的问题,他们希望拆分字符串,而不会在返回时获得空结果。使用正则表达式大大简化了这一点。首先添加对 Microsoft VBScript Regular Expressions 5.5 的引用。然后,您可以根据您的特定需求调整以下功能。

请注意,示例中没有错误处理。

Private Function SplitString(ByVal vPattern As String, ByVal vText As String, ByRef Result() As String) As Integer
    Dim regex As New RegExp
    Dim colMatches As MatchCollection
    Dim intMatchCount As Integer
    Dim i As Integer

    intMatchCount = 0
    regex.Pattern = vPattern
    regex.Global = True
    regex.IgnoreCase = True
    Set colMatches = regex.Execute(vText)
    If regex.Test(vText) = True Then
        intMatchCount = colMatches.Count
        ReDim Result(0 To intMatchCount)
        For i = 0 To intMatchCount - 1
            Result(i) = colMatches(i).Value
        Next i
        Set colMatches = Nothing    ' I don't know if this is needed, but playing it safe
    End If
    Set regex = Nothing    ' I don't know if this is needed, but playing it safe

    SplitString = intMatchCount

End Function

要使用此功能,请在表单中添加多行文本框和命令按钮,并粘贴到以下代码中。

Private Sub Command1_Click()
    Dim aryMatches() As String
    Dim i As Integer
    Dim strPattern As String
    Dim strText As String

    Text1.Text = ""
    strPattern = "\w+"
    strText = "NODE TEST              ACCESS"
    If SplitString(strPattern, strText, aryMatches) > 0 Then
        For i = LBound(aryMatches) To UBound(aryMatches)
            Text1.SelText = aryMatches(i) & vbCrLf
            Text1.SelStart = Len(Text1.Text)
        Next i
    End If

End Sub

答案 2 :(得分:2)

只需使用正则表达式将两个或多个空格替换为任何字符,然后拆分该字符。

  1. 添加对“Microsoft VBScript正则表达式”的引用。
  2. 做这样的事情:

    Dim a() As String
    
    With New RegExp
        .Pattern = "\s{2,}"
        a = Split(.Replace(someStr, "~"), "~")
    End With
    

答案 3 :(得分:1)

如果您不需要保留空格,可以尝试使用replace命令将每两个字符替换为可以拆分的另一个字符

Dim someStr As String
Dim someArray() as String

someStr = "NODE TEST              ACCESS"
someArray = split(Replace$(someStr,"  ","|"),"|")

答案 4 :(得分:1)

对lardymonkey和BoBrodes的答案略有改变

为什么要用" |"替换空格? ?原始字符串可以包含" |"本身会产生意想不到的结果

最好用单个替换双空格:

Private Sub Command1_Click()
  Dim someStr As String
  Dim strArray() As String
  someStr = "NODE TEST              ACCESS"
  someStr = RemoveDouble(someStr, " ")
  strArray = Split(someStr, " ")
End Sub

Private Function RemoveDouble(strSource As String, strRemove As String)
  Dim strReturn As String
  Dim strDouble As String
  strDouble = strRemove & strRemove
  strReturn = Replace(strSource, strDouble, strRemove)
  Do While InStr(strReturn, strDouble) > 0
    strReturn = Replace(strReturn, strDouble, strRemove)
  Loop
  RemoveDouble = strReturn
End Function

答案 5 :(得分:1)

这一次,一个完全不同的答案 - 使用更多"原语" VB字符串函数。如果你对这些事情感兴趣,这大约是Bob Rhode的答案的两倍。

基本上,我在字符串中移动,注意两个或多个空格的位置,然后一次移动一个字符,直到找到非空格。使用此信息,我们可以从字符串中的正确位置拉出子串,并将它们复制到预先分配的字符串数组中。我以64块为单位分配数组。如果我们超过数组中的元素数量,我们将其重新分配给另一个64块。

Private Function SplitOnMultiSpaces2(ByVal someStr As String) As String()

    Const someStringsChunkLen   As Long = 64
    Dim someStringLen           As Long
    Dim someStrings()           As String
    Dim someStringsIndex        As Long
    Dim multiSpacePos           As Long
    Dim nextPos                 As Long

    ' Cache the length of the string.
    someStringLen = Len(someStr)

    ' Allocate one chunk of elements initially.
    ReDim someStrings(0 To someStringsChunkLen - 1)

    ' Point to the first element in the array.
    someStringsIndex = 0

    ' Find the first position of more than 1 space.
    multiSpacePos = InStr(1, someStr, "  ", vbBinaryCompare)

    ' Special case. If no multi spaces were found, then simply return a single string in the array.
    If multiSpacePos = 0 Then
        someStrings(0) = someStr
    Else
        ' Point the beginning of the next string to the first character in <someStr>.
        nextPos = 1
        Do
            ' Copy the "next string" into the next available array element.
            someStrings(someStringsIndex) = Mid$(someStr, nextPos, multiSpacePos - nextPos)

            ' Move to the second space in the multi-spaces, and iterate until we find a non-space (space = ASCII 32).
            nextPos = multiSpacePos + 1
            Do
                If nextPos = someStringLen Then
                    Exit Do
                End If
                nextPos = nextPos + 1
            Loop While AscW(Mid$(someStr, nextPos, 1)) = 32

            ' We now pointing to the beginning of the next string - or at the end of the string.
            ' Look for the next multi space.
            multiSpacePos = InStr(nextPos, someStr, "  ", vbBinaryCompare)

            ' Point to the next array element.
            someStringsIndex = someStringsIndex + 1
            ' If this array element points beyond the current upper bound of the array, then add another chunk to the array.
            ' We look at the remainder from dividing <someStringsIndex> by <someStringsChunkLen>.
            ' For instance, if this is element 64, then this is 64 / 64 = 1 remainder 0.
            ' We can use this simple test because we only resize upwards.
            If (someStringsIndex Mod someStringsChunkLen) = 0 Then
                ' e.g. resize upper bound to 64 + 64 - 1 = 127.
                ReDim Preserve someStrings(0 To someStringsIndex + someStringsChunkLen - 1)
            End If
        Loop Until multiSpacePos = 0

        ' If we aren't at the end of the string, then copy the remaining values.
        If nextPos <> someStringLen Then
            someStrings(someStringsIndex) = Mid$(someStr, nextPos)
        End If
    End If

    ' Resize down to the proper size.
    ReDim Preserve someStrings(0 To someStringsIndex)

    ' Return the string array.
    SplitOnMultiSpaces2 = someStrings()

End Function