vb.net: can you split a string by a string

53,804

Solution 1

Try this:

string[] myDelims = new string[] { "<beginning of record>" };
split = temp_string.Split(myDelims,StringSplitOptions.None);

Running this through a code converter results in this:

Dim myDelims As String() = New String() { "<beginning of record>" }
split = temp_string.Split(myDelims, StringSplitOptions.None)

You may also need to escape the chevrons, like this:

"\< beginning of record \>"

Solution 2

@Matthew Jones' answer in VB.NET

Dim delim as String() = New String(0) { "<beginning of record>" } 
split = temp_string.Split(delim, StringSplitOptions.None)

Solution 3

You can write yourself an extension method to make it easier (Based on the answer by Matthew Jones)

(guess I should show an example as well...)

Dim results = "hay needle hay needle hay".Split("needle")
' results(0) = "hay "
' results(1) = " hay "
' results(2) = " hay"

... C# ...

public static class Tools
{
    public static string[] Split(this string input, params string[] delimiter)
    {
        return input.Split(delimiter, StringSplitOptions.None);
    }
}

... VB.Net ...

Module Tools
    <Extension()> _
    Public Function Split(ByVal input As String, _
                          ByVal ParamArray delimiter As String()) As String()
        Return input.Split(delimiter, StringSplitOptions.None)
    End Function
End Module

Solution 4

You could look at Regex.Split()-method.

And this seems to work

  dim s as string = "you have a <funny> thing <funny> going on"
  dim a() as string = Regex.Split(s,"<funny>")
  for each b as string in a 
     Response.Write( b & "<br>")
  next

Solution 5

this seem to work

    Dim myString As String = "aaajjbbbjjccc"
    Dim mySplit() As Char = "jj".ToCharArray
    Dim myResult() As String = myString.Split(mySplit, StringSplitOptions.RemoveEmptyEntries)
Share:
53,804
Alex Gordon
Author by

Alex Gordon

Check out my YouTube channel with videos on Azure development.

Updated on February 21, 2020

Comments

  • Alex Gordon
    Alex Gordon about 4 years

    for example, can i do this:

    split = temp_string.Split("<beginning of record>")
    

    those of you recommended:

    split = Regex.Split(temp_string, "< beginning of record >")
    

    its not working. its just returning the first char "<"

    and those of you that recommended:

    Dim myDelims As String() = New String(){"< beginning of record >"}
    split = temp_string.Split(myDelims, StringSplitOptions.None)
    

    this is not working either. it's also returning just the first char