Hey, I'm using this code:
which gives me two strings, data(0) and data(1), but I want to be able to get data(2) etc.Code:Dim data() As String = Message.Split("|"c)
any ideas on how to do this?
Thanks
Printable View
Hey, I'm using this code:
which gives me two strings, data(0) and data(1), but I want to be able to get data(2) etc.Code:Dim data() As String = Message.Split("|"c)
any ideas on how to do this?
Thanks
Can't see you're code?
Sorry, fixed now
I'm assuming that the c) at the end of your Message.Split is a typo. Read this: http://msdn.microsoft.com/en-us/libr...ing.split.aspxQuote:
Originally Posted by want a pie
Your method will return more than 2 strings, if the string you've declared as Message actually has more than one "|" character in it, which is what you've declared as your seperator.
Using this code as an example:
vb.net Code:
Dim Message As String = "Apple|Banana|Carrot|Etc" Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim data() As String = Message.Split(Convert.ToChar("|")) For i As Integer = 0 To (data.Length - 1) Debug.WriteLine(data(i).ToString) Next End Sub
I get this output into the 'Immediate Window' of the IDE:
vb.net Code:
Apple Banana Carrot Etc
Which you could also access directly with Data(0), Data(1), Data(2) etc, even if there were 100.
I'm not sure if that helps because I can't what your problem is exactly with the small amount of code you've provided...
Let me know.
Cheers,
Scott
No it isn't. It's your code that's wrong. In your code you use "|", which is a String, while the OP used "|"c, which is a Char. As your own link shows, there is no overload of String.Split that takes a single String argument. Your code is relying on the String being implicitly converted to a Char, which is a compilation error with Option Strict On. The first overload of String.Split takes a ParamArray of Chars, which means it will accept zero or more Char values or one Char array.Quote:
Originally Posted by scootabug
Damn...see what happens when I try to help, John!Quote:
Originally Posted by jmcilhinney
I have never seen "|"c this before, so I shouldn't have assumed. I've just tried it again and yeah, I stand well corrected. All the same, my code still gives the correct result using "|"c as the seperator. I've always done it the way my code has shown without a problem (doesn't mean it's correct, just means I've gotten away with it!)
So what is this "c" notation referred to as? I need to some reading apparently...
When you say that it is a compilation error with Option Strict On. I've got Option Strict On (only just put it on recently) and I don't get any errors using Convert.ToChar, which I only chucked in there because I got an error when NOT using it. Or is that the error you're referring to?
Nevermind, found it. Seems a tad over the top when it "looks" like the same thing. "Z" vs "Z"?Quote:
Originally Posted by scootabug
vb.net Code:
Option Strict On Dim charVar As Char ' The following statement attempts to convert a String literal to Char. ' Because Option Strict is On, it generates a compiler error. charVar = "Z" ' The following statement succeeds because it specifies a Char literal. charVar = "Z"C
Sorry to hijack the thread...
Yup, I think that's what he's referring to. c for character, so instead of writing Convert.ToChar you just place c ;-)Quote:
When you say that it is a compilation error with Option Strict On. I've got Option Strict On (only just put it on recently) and I don't get any errors using Convert.ToChar, which I only chucked in there because I got an error when NOT using it. Or is that the error you're referring to?
Sweet. Another small piece of knowledge in the bag. Thanks!