|
-
Nov 23rd, 2008, 12:12 AM
#1
Thread Starter
Lively Member
[2008] Split a string into several pieces
Hey, I'm using this code:
Code:
Dim data() As String = Message.Split("|"c)
which gives me two strings, data(0) and data(1), but I want to be able to get data(2) etc.
any ideas on how to do this?
Thanks
Last edited by want a pie; Nov 23rd, 2008 at 12:31 AM.
-
Nov 23rd, 2008, 12:17 AM
#2
Addicted Member
Re: [2008] Split a string into several pieces
-
Nov 23rd, 2008, 12:36 AM
#3
Thread Starter
Lively Member
Re: [2008] Split a string into several pieces
-
Nov 23rd, 2008, 03:44 AM
#4
Addicted Member
Re: [2008] Split a string into several pieces
 Originally Posted by want a pie
Hey, I'm using this code:
Code:
Dim data() As String = Message.Split("|"c)
which gives me two strings, data(0) and data(1), but I want to be able to get data(2) etc.
any ideas on how to do this?
Thanks
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.aspx
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:
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
-
Nov 23rd, 2008, 04:20 AM
#5
Re: [2008] Split a string into several pieces
 Originally Posted by scootabug
I'm assuming that the c) at the end of your Message.Split is a typo.
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.
-
Nov 23rd, 2008, 04:31 AM
#6
Addicted Member
Re: [2008] Split a string into several pieces
 Originally Posted by jmcilhinney
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.
Damn...see what happens when I try to help, John!
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?
-
Nov 23rd, 2008, 04:34 AM
#7
Addicted Member
Re: [2008] Split a string into several pieces
 Originally Posted by scootabug
So what is this "c" notation referred to as? I need to some reading apparently...
Nevermind, found it. Seems a tad over the top when it "looks" like the same thing. "Z" vs "Z"?
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...
-
Nov 23rd, 2008, 08:43 AM
#8
Junior Member
Re: [2008] Split a string into several pieces
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?
Yup, I think that's what he's referring to. c for character, so instead of writing Convert.ToChar you just place c ;-)
-
Nov 23rd, 2008, 03:47 PM
#9
Addicted Member
Re: [2008] Split a string into several pieces
Sweet. Another small piece of knowledge in the bag. Thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|