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 eit开发者_运维问答her. it's also returning just the first char
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' answer in VB.NET
Dim delim as String() = New String(0) { "<beginning of record>" }
split = temp_string.Split(delim, StringSplitOptions.None)
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
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
this seem to work
Dim myString As String = "aaajjbbbjjccc"
Dim mySplit() As Char = "jj".ToCharArray
Dim myResult() As String = myString.Split(mySplit, StringSplitOptions.RemoveEmptyEntries)
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.
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.
精彩评论