35

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

Alex Gordon
  • 57,446
  • 287
  • 670
  • 1,062
  • I think the problem is that you need to escape the chevrons. See my edit. – Matthew Jones Oct 28 '09 at 18:51
  • 1
    If you continue to have problems you might want to give an example of that is in temp_string. Your sample is working fine for temp_string="before< beginning of record >after" – Matthew Whited Oct 28 '09 at 19:23
  • Regex.split worked for me. The char array option with String.split seems like it should split at every reference of <,b,e,g,i,n,n,i,n,g, etc. individually rather than as a whole. – Jono Oct 09 '12 at 22:06

7 Answers7

42

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 \>"
Ralph Willgoss
  • 11,750
  • 4
  • 64
  • 67
Matthew Jones
  • 25,644
  • 17
  • 102
  • 155
  • In my case, it didn't work until I added the option StringSplitOptions.None. Without it, VS tells me the string can't be converted to char – Rafiki Aug 03 '18 at 08:49
  • 1
    Also, a way to write this in line is: Split = temp_string.Split({""}, StringSplitOptions.None) – Rafiki Aug 03 '18 at 08:59
18

@Matthew Jones' answer in VB.NET

Dim delim as String() = New String(0) { "<beginning of record>" } 
split = temp_string.Split(delim, StringSplitOptions.None)
Community
  • 1
  • 1
bdukes
  • 152,002
  • 23
  • 148
  • 175
7

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
Community
  • 1
  • 1
Matthew Whited
  • 22,160
  • 4
  • 52
  • 69
6

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
  • 4,120
  • 2
  • 27
  • 20
3

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
  • 19,848
  • 10
  • 58
  • 113
2

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
  • 624
  • 4
  • 3
1

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
  • 2,447
  • 1
  • 15
  • 26
  • split = Regex.Split(temp_string, "")? – Alex Gordon Oct 28 '09 at 18:40
  • maybe something like this? pattern = "" Dim r As Regex r = 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 Oct 28 '09 at 18:44
  • If you're going to use `Regex.Split`, use `Regex.Escape` on what you'll be matching against. – bdukes Oct 28 '09 at 22:05