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

.Net-我应该使用Split函数还是Split方法?,.net,vb.net,windows-mobile,compact-framework,.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 A

背景:

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

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
因此,我的代码大致如下所示:

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

这是不起作用的,因为分裂函数没有把我的分隔符看作单个字符

。 然而这起作用了

''//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开发,这就是为什么速度和内存问题变得重要的原因


谢谢。

以下是为这两种方法执行的代码;你自己决定吧。我更喜欢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
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
Private函数internalsplitkeepmptyentries(ByVal sepList为Integer()、ByVal lengthList为Integer()、ByVal numReplaces为Integer、ByVal count为Integer)作为字符串()
Dim startIndex作为整数=0
作为整数的Dim索引=0
计数-=1
整数形式的Dim num3=IIf((numReplaces<计数),numReplaces,计数)
Dim strArray As String()=新字符串((num3+1)-1){}
尺寸i为整数=0
执行While((i=0)),则
strArray(索引)=Me.子字符串(startIndex)
回程跑道
如果结束
如果(指数=num3),则
strArray(索引)=字符串。空
如果结束
回程跑道
端函数
下面是调用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
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
私有共享函数SplitHelper(ByVal sSrc作为字符串,ByVal sFind作为字符串,ByVal cMaxSubStrings作为整数,ByVal[Compare]作为整数)作为字符串()
Dim不变量CompareInfo作为CompareInfo
作为整数的Dim num2
Dim序数作为比较项
将长度设置为整数
作为整数的Dim num5
作为整数的Dim num6
如果(sFind什么都不是)那么
长度=0
其他的
长度=固定长度
如果结束
如果(sSrc不算什么),那么
num6=0
其他的
num6=sSrc.长度
如果结束
如果(长度=0),则
返回新字符串(){sSrc}
如果结束
如果(num6=0),则
返回新字符串(){sSrc}
如果结束
Dim num作为整数=20
如果(num>cMaxSubStrings),则
num=cMaxSubStrings
如果结束
Dim strArray As String()=新字符串((num+1)-1){}
如果([比较]=0),则
序号=比较选项。序号
invariantCompareInfo=Strings.m_invariantCompareInfo
其他的
invariantCompareInfo=Utils.GetCultureInfo.CompareInfo
序号=(CompareOptions.IgnoreWidth或(CompareOptions.IgnoreKanaType或CompareOptions.IgnoreCase))
如果结束
边做边做(num5num),则
num=(num+20)
如果(num>cMaxSubStrings),则
num=(cMaxSubStrings+1)
如果结束
strArray=DirectCast(Utils.CopyArray(DirectCast(strArray,Array)),新字符串((num+1)-1{}),字符串()
如果结束
strArray(num2)=“
如果(num2=cMaxSubStrings),则
str=sSrc.子字符串(num5)
如果(str什么都不是)那么
str=“”
如果结束
strArray(num2)=str
退出Do
如果结束
环
如果((num2+1)=直线长度),则
回程跑道
如果结束
返回DirectCast(Utils.CopyArray(DirectCast(strArray,Array),新字符串((num2+1)-1{}),String()
端函数

以下是为这两种代码执行的代码;你自己决定吧。我更喜欢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
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
Private函数internalsplitkeepmptyentries(ByVal sepList为Integer()、ByVal lengthList为Integer()、ByVal numReplaces为Integer、ByVal count为Integer)作为字符串()
Dim startIndex作为整数=0
作为整数的Dim索引=0
计数-=1
整数形式的Dim num3=IIf((numReplaces<计数),numReplaces,计数)
Dim strArray As String()=新字符串((num3+1)-1){}
尺寸i为整数=0
执行While((i=0)),则
strArray(索引)=Me.子字符串(startIndex)
回程跑道
如果结束
如果(指数=num3),则
strArray(索引)=Strin