代码之家  ›  专栏  ›  技术社区  ›  moster67

.Net-我应该使用Split函数还是Split方法?

  •  0
  • moster67  · 技术社区  · 16 年前

    背景

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

    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 回复  |  直到 16 年前
        1
  •  2
  •   Rex M    16 年前

    我会使用正则表达式

    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()
    

    输出:

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

        2
  •  2
  •   cgreeno    16 年前

    这是为两者执行的代码;你自己决定。我更喜欢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
    
    推荐文章