views:

335

answers:

2

Background:

I needed to split a string of numerous words into an array which separates the words for me for further use later on in my code. However, I needed to get rid of any numbers which might be present in the string so I declared a string contaning characters which I wanted to use as separators/delimiters as follows:

dim Separators As String = " 1234567890"

so my code became more or less as follows:

''//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

This did not work since the Split-functions did not consider my Separators as individual characters

however this worked..

''//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

So far, so good - since one has more options with the Split-method compared to the Split-function, I managed to resolve my problem.

Now to my question, let's say I only needed the standard space-character (" ") as a delimiter (and no need to get rid of numbers like in above example), then both ways would work. So which one would you use? Apart from various options available, are there any advantages using a particular one? Perhaps one is faster, less memory-hungry?

EDIT: I'm developing for Windows Mobile and that's why speed- and memory-issues get important.

Thank you.

+2  A: 

Here's the code that gets executed for both; decide for yourself. I like string.split() more.

Here is the code that gets executed when you call 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

Here is the code that gets executed when you call the Split() 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
Rex M
+2  A: 

I would use regular expressions

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

OUTPUT:

This is a test this is another test and again and one more

cgreeno
Regex is certainly the way to go here.
ctacke
To cstacke: I know that you're a "guru" what regards WinMobile...is this the reason why you suggest Regex (i.e. better suited for speed- and memory-issues than the Split-method and the Split-function)?
moster67
No, I recommend Regex becssue it's just way cleaner and way simpler to achieve the end goal. I'd do the same on the desktop as well.
ctacke