Alex Gordon
Alex Gordon

Reputation: 60871

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

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

Upvotes: 35

Views: 60417

Answers (7)

Matthew Jones
Matthew Jones

Reputation: 26190

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 \>"

Upvotes: 42

Matthew Whited
Matthew Whited

Reputation: 22443

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

Upvotes: 7

Erik
Erik

Reputation: 4105

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

Upvotes: 7

Fredou
Fredou

Reputation: 20140

this seem to work

    Dim myString As String = "aaajjbbbjjccc"
    Dim mySplit() As Char = "jj".ToCharArray
    Dim myResult() As String = myString.Split(mySplit, StringSplitOptions.RemoveEmptyEntries)

Upvotes: 3

inked
inked

Reputation: 624

If what you're really splitting is XML read into a string, then don't use VB strings to do that work. Use XSLT. VB/C# have methods for rendering XML with XSLT. It'll be much quicker and more reliable.

Upvotes: 2

bdukes
bdukes

Reputation: 156055

@Matthew Jones' answer in VB.NET

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

Upvotes: 18

Brienne Schroth
Brienne Schroth

Reputation: 2457

I don't think so, it only takes characters. You could do some ugly hack work around and first replace all instance of the string with some character that does not already exist in the string, and then to Split on that character.

Edited to add: I think Regex.Split is able to do the split on a regex, so if you make a simple regex that is simply the string you want to split on, that should work.

Upvotes: 1

Related Questions