views:

843

answers:

7

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

+5  A: 

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 \>"
Matthew Jones
matt can i get that in vb.net format please
I__
please see edit
I__
+1  A: 

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.

Brian Schroth
brian can u please give me the code for that
I__
split = Regex.Split(temp_string, "<beginning of record>")?
I__
maybe something like this?pattern = "<beginning of record>"Dim r As Regexr = new Regex(pattern)Dim results As String()results = r.Split(temp_string)might work...I don't know vb or anything, just a guess based on documentation
Brian Schroth
please see edit
I__
If you're going to use `Regex.Split`, use `Regex.Escape` on what you'll be matching against.
bdukes
+1  A: 

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
Erik
please see edit
I__
+3  A: 

@Matthew Jones' answer in VB.NET

Dim delim as String() = New String(0) { "<beginning of record>" } 
split = temp_string.Split(delim, StringSplitOptions.None)
bdukes
+1 because you beat me to it
Russ Bradberry
please see edit
I__
+2  A: 

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.

inked
+1  A: 

this seem to work

    Dim myString As String = "aaajjbbbjjccc"
    Dim mySplit() As Char = "jj".ToCharArray
    Dim myResult() As String = myString.Split(mySplit, StringSplitOptions.RemoveEmptyEntries)
Fredou
+2  A: 

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
Matthew Whited