如何在groovy中拆分由空格分隔的String

时间:2017-05-09 21:31:43

标签: string groovy

我正在尝试使用空格分隔我的String并提取String值的第二个文本。

这是我的代码 -

Option Explicit

Sub DSC()
    Dim mWB As Workbook
    Dim masterWorkbook As String, c As Long, rowCount As Long, serial As Variant
    Dim k As Long, dict As Object

    Set dict = CreateObject("Scripting.Dictionary")
    dict.comparemode = vbTextCompare

    With ThisWorkbook.Worksheets("Update Wipe")
        For k = 2 To .Cells(.Rows.Count, "A").End(xlUp).Row
            dict(.Cells(k, "A").Value2) = vbNullString
        Next k
    End With

    masterWorkbook = "Master.xlsx"
    Set mWB = Workbooks.Open(ThisWorkbook.Path & "\" & masterWorkbook)

    With mWB.Worksheets("Sheet1")
        If .AutoFilterMode Then .AutoFilterMode = False
        With .Cells(1, "A").CurrentRegion
            .AutoFilter field:=1, Criteria1:=dict.keys, Operator:=xlFilterValues
            With .Resize(.Rows.Count - 1, 5).Offset(1, 0)
                If CBool(Application.Subtotal(103, .Cells)) Then
                    .Columns("E").SpecialCells(xlCellTypeVisible) = "DSC"
                End If
            End With
        End With
        If .AutoFilterMode Then .AutoFilterMode = False
        'optionally close the master workbook
        '.Parent.Close savechanges:=True
    End With
End Sub

这是我得到的输出 - [a,b,c,d,。,a,b,c,。,c,d,e,。,f,g,h]

我正在寻找的输出是abcd.abc.cde.fgh,但我得到[a,b,c,d,。,a,b,c,。,c,d,e,。, f,g,h]

我使用过(“\ s +”),(“\ s”),只是括号内的空格,包含空格的单引号和双引号,没有任何作用。

1 个答案:

答案 0 :(得分:4)

因为这里

String[] newTest = test[1]

你告诉groovy将你想要的字符串粘贴到字符串数组

所以groovy尝试做你说的话,并将字符拆分成单独的字符串

你想要的只是一个字符串,而不是一个字符串数组。尝试

String newTest = test[1]

相反