.Net-我应该使用Split-function还是split-method?

时间:2009-02-07 14:59:13

标签: .net vb.net windows-mobile compact-framework

背景:

我需要将一串多个单词分成一个数组,该数组为我分隔单词,以便以后在我的代码中进一步使用。但是,我需要删除字符串中可能存在的任何数字,因此我声明了一个包含字符的字符串,我想用它作为分隔符/分隔符,如下所示:

dim Separators As String = " 1234567890"

所以我的代码或多或少如下:

''//USING SPLIT-FUNCTION
dim MyTestString as String = "This is 9 a 567 test" 
dim Separators As String = " 1234567890"
dim ResultWithNoNumbers() as String

ResultWithNoNumbers = Split(MyTestString,Separators.ToCharArray)
Messagebox.Show(ResultWithNoNumbers.Length.ToString)
''//Result is 1 and no split was performed

这不起作用,因为Split功能不将我的分隔符视为单个字符

然而这有效..

''//USING SPLIT-METHOD
dim MyTestString as String = "This is 9 a 567 test" 
dim Separators As String = " 1234567890"
dim ResultWithNoNumbers() as String

ResultWithNoNumbers = MyTestString.Split(Separators.ToCharArray,
                                    _StringSplitOptions.RemoveEmptyEntries)
Messagebox.Show(ResultWithNoNumbers.Length.ToString)
''//Result is 4 and split was performed

到目前为止,非常好 - 因为与Split功能相比,Split方法有更多选项,我设法解决了我的问题。

现在我的问题,让我说我只需要标准空格字符(“”)作为分隔符(并且不需要像上面的例子那样去除数字),然后两种方式都可以。你会用哪一个?除了各种选择外,使用特定的选项还有什么优势吗?也许一个更快,更少记忆?

编辑:我正在为Windows Mobile开发,这就是速度和内存问题变得重要的原因。

谢谢。

2 个答案:

答案 0 :(得分:2)

这是为两者执行的代码;自行决定。我更喜欢string.split()。

以下是调用 string.split()时执行的代码:

Private Function InternalSplitKeepEmptyEntries(ByVal sepList As Integer(), ByVal lengthList As Integer(), ByVal numReplaces As Integer, ByVal count As Integer) As String()
    Dim startIndex As Integer = 0
    Dim index As Integer = 0
    count -= 1
    Dim num3 As Integer = IIf((numReplaces < count), numReplaces, count)
    Dim strArray As String() = New String((num3 + 1)  - 1) {}
    Dim i As Integer = 0
    Do While ((i < num3) AndAlso (startIndex < Me.Length))
        strArray(index++) = Me.Substring(startIndex, (sepList(i) - startIndex))
        startIndex = (sepList(i) + IIf((lengthList Is Nothing), 1, lengthList(i)))
        i += 1
    Loop
    If ((startIndex < Me.Length) AndAlso (num3 >= 0)) Then
        strArray(index) = Me.Substring(startIndex)
        Return strArray
    End If
    If (index = num3) Then
        strArray(index) = String.Empty
    End If
    Return strArray
End Function

以下是调用 Split()函数时执行的代码:

Private Shared Function SplitHelper(ByVal sSrc As String, ByVal sFind As String, ByVal cMaxSubStrings As Integer, ByVal [Compare] As Integer) As String()
    Dim invariantCompareInfo As CompareInfo
    Dim num2 As Integer
    Dim ordinal As CompareOptions
    Dim length As Integer
    Dim num5 As Integer
    Dim num6 As Integer
    If (sFind Is Nothing) Then
        length = 0
    Else
        length = sFind.Length
    End If
    If (sSrc Is Nothing) Then
        num6 = 0
    Else
        num6 = sSrc.Length
    End If
    If (length = 0) Then
        Return New String() { sSrc }
    End If
    If (num6 = 0) Then
        Return New String() { sSrc }
    End If
    Dim num As Integer = 20
    If (num > cMaxSubStrings) Then
        num = cMaxSubStrings
    End If
    Dim strArray As String() = New String((num + 1)  - 1) {}
    If ([Compare] = 0) Then
        ordinal = CompareOptions.Ordinal
        invariantCompareInfo = Strings.m_InvariantCompareInfo
    Else
        invariantCompareInfo = Utils.GetCultureInfo.CompareInfo
        ordinal = (CompareOptions.IgnoreWidth Or (CompareOptions.IgnoreKanaType Or CompareOptions.IgnoreCase))
    End If
    Do While (num5 < num6)
        Dim str As String
        Dim num4 As Integer = invariantCompareInfo.IndexOf(sSrc, sFind, num5, (num6 - num5), ordinal)
        If ((num4 = -1) OrElse ((num2 + 1) = cMaxSubStrings)) Then
            str = sSrc.Substring(num5)
            If (str Is Nothing) Then
                str = ""
            End If
            strArray(num2) = str
            Exit Do
        End If
        str = sSrc.Substring(num5, (num4 - num5))
        If (str Is Nothing) Then
            str = ""
        End If
        strArray(num2) = str
        num5 = (num4 + length)
        num2 += 1
        If (num2 > num) Then
            num = (num + 20)
            If (num > cMaxSubStrings) Then
                num = (cMaxSubStrings + 1)
            End If
            strArray = DirectCast(Utils.CopyArray(DirectCast(strArray, Array), New String((num + 1)  - 1) {}), String())
        End If
        strArray(num2) = ""
        If (num2 = cMaxSubStrings) Then
            str = sSrc.Substring(num5)
            If (str Is Nothing) Then
                str = ""
            End If
            strArray(num2) = str
            Exit Do
        End If
    Loop
    If ((num2 + 1) = strArray.Length) Then
        Return strArray
    End If
    Return DirectCast(Utils.CopyArray(DirectCast(strArray, Array), New String((num2 + 1)  - 1) {}), String())
End Function

答案 1 :(得分:2)

我会使用正则表达式

Dim MyTestString As String = "This is 9 a 567 test 23424234 this is 
23 another test 23 and 3 again and one 34234 more"
Dim reg_exp As New Text.RegularExpressions.Regex("\d")
Dim reg_exp2 As New Text.RegularExpressions.Regex("\s{2,}")

MyTestString = reg_exp.Replace(MyTestString, String.Empty)
MyTestString = reg_exp2.Replace(MyTestString, " ")

Dim strArr() As String
strArr = MyTestString.ToString.Split(" ")

Console.WriteLine(MyTestString)
Console.ReadLine()

输出:

这是一个测试,这是另一个测试,又一次又一次