*Experts* Bucky Posted November 15, 2002 *Experts* Posted November 15, 2002 In the never-ending quest to ditch the old VB6 way of doing things, I'm now trying to figure out how to use String.Split like the old Split$ function. Using Split$, a delimeter could be another String, not only one character. The String.Split method, however, only wants to take a Char as a delimeter. I tried converting a string into a Char array, but that treated each and every Char as a delimeter. Oops. Nothing is wrong with using Split$, and I'm forced into using it for now, but as I mentioned, the world would be a much better place if we all got along and used VB.NET's features. :) So how can I use String.Split and pass another String as a delimeter? Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
*Gurus* divil Posted November 15, 2002 *Gurus* Posted November 15, 2002 Dim x As String = "I, like, cheese" Dim y As String() = x.Split(", ".ToCharArray) You'll get "I", "", "like", "", "cheese" Don't really know why is puts blanks in there like it does. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Leaders quwiltw Posted November 15, 2002 Leaders Posted November 15, 2002 Can you post what your trying? I just had a crack at it and it seems to work with one quirk. Dim delimiter As String delimiter = "***" Dim myString As String myString = "hello***world***again***trying" Dim strAry As String() strAry = myString.Split(delimiter.ToCharArray) The only problem with what I have is that it splits into 10 elements instead of 4 but this is my first try. Quote --tim
Leaders quwiltw Posted November 15, 2002 Leaders Posted November 15, 2002 Awww Shucks... looks like I've been beaten to the punch... and you got the same extra elements I got. Quote --tim
Vitaly Posted November 15, 2002 Posted November 15, 2002 There is one way of doing this, the result is correct, but I don't know if it's a "clean" enough solution: Dim sTest1 As String = "Good****morning****afternoon****and night" Dim y As String() = sTest1.Replace("****", Chr(0)).Split(Chr(0)) Quote
*Experts* Bucky Posted November 15, 2002 Author *Experts* Posted November 15, 2002 Dim toSplit As String = "The[split]quick brown fox[split]jumped over the[split]lazy dog" toSplit.Split("[split]".ToCharArray()) returns 26 array items, because each character is counted as a delimeter, and if any one of those characters is encountered, the string splits there (see below). Should I just stick to Split$??? :) (0): "The" (1): "" (2): "" (3): "" (4): "" (5): "" (6): "" (7): "qu" (8): "ck brown fox" (9): "" (10): "" (11): "" (12): "" (13): "" (14): "" (15): "jum" (16): "ed over " (17): "he" (18): "" (19): "" (20): "" (21): "" (22): "" (23): "" (24): "" (25): "azy dog" Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
*Gurus* Derek Stone Posted November 15, 2002 *Gurus* Posted November 15, 2002 You could create your own Split function. We can't expect the framework to do it all you know. :) Quote Posting Guidelines
*Experts* Bucky Posted November 15, 2002 Author *Experts* Posted November 15, 2002 I'll consider it, although using Split$ is much more convenient (sp?) for the moment. Thanks for your ideas, everyone. [edit]Actually, Vitaly, I really like your idea, and I'll probably stick in a function and use it. :) Thanks.[/edit] Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
wyrd Posted November 15, 2002 Posted November 15, 2002 In Java there's a nice class named StringTokenizer. To be honest, I'm surprised that .NET doesn't have anything similar which is why I bring it up (.NET seems to take a lot of elements from Java in terms of the assemblies available compared to Java packages). I only bring this up incase you were going to build your own Split function. It may give you some ideas. If I knew more about .NET I'd go ahead and do it myself. :-\ http://java.sun.com/j2se/1.4.1/docs/api/java/util/StringTokenizer.html Quote Gamer extraordinaire. Programmer wannabe.
*Gurus* divil Posted November 15, 2002 *Gurus* Posted November 15, 2002 Ideally in .NET you'd use Regular Expressions to do this sort of thing. They seem to have focussed the attention more towards these in the framework and examples I have seen. There is a lot of support for them. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
wyrd Posted November 22, 2002 Posted November 22, 2002 Divil is right about the heavy support for Regular Expressions. I finally got a chance to learn about them (at least enough to start doing basing things). Splitting words using a delim is easy enough using RegEx. I wrote up a quick console application that demonstrates the ease of use. 'To shorten code below that uses this assembly. Imports System.Text.RegularExpressions Module Module1 Sub Main() 'Sentence to split. Dim str As String = "Some[*split*]sentence[*split*]to[*split*]split." Dim delim As String = "[*split*]" 'Delim to use. delim = Regex.Escape(delim) 'Be sure to add escape chars. 'Split the string using our delim. Dim splitStr As String() = Regex.Split(str, delim) Console.WriteLine(splitStr.Length) 'Print out the number of words. 'Print out each word. Dim word As String For Each word In splitStr Console.WriteLine(word) Next Console.ReadLine() End Sub End Module Quote Gamer extraordinaire. Programmer wannabe.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.