Results 1 to 9 of 9

Thread: [2008] Split a string into several pieces

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2007
    Posts
    73

    Question [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.

  2. #2
    Addicted Member
    Join Date
    Mar 2008
    Posts
    150

    Re: [2008] Split a string into several pieces

    Can't see you're code?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2007
    Posts
    73

    Re: [2008] Split a string into several pieces

    Sorry, fixed now

  4. #4
    Addicted Member
    Join Date
    Mar 2008
    Posts
    150

    Re: [2008] Split a string into several pieces

    Quote 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:
    1. Dim Message As String = "Apple|Banana|Carrot|Etc"
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.  
    5.         Dim data() As String = Message.Split(Convert.ToChar("|"))
    6.  
    7.         For i As Integer = 0 To (data.Length - 1)
    8.             Debug.WriteLine(data(i).ToString)
    9.         Next
    10.  
    11.     End Sub

    I get this output into the 'Immediate Window' of the IDE:
    vb.net Code:
    1. Apple
    2. Banana
    3. Carrot
    4. 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

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Split a string into several pieces

    Quote 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Addicted Member
    Join Date
    Mar 2008
    Posts
    150

    Re: [2008] Split a string into several pieces

    Quote 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?

  7. #7
    Addicted Member
    Join Date
    Mar 2008
    Posts
    150

    Re: [2008] Split a string into several pieces

    Quote 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:
    1. Option Strict On
    2. Dim charVar As Char
    3. ' The following statement attempts to convert a String literal to Char.
    4. ' Because Option Strict is On, it generates a compiler error.
    5. charVar = "Z"
    6. ' The following statement succeeds because it specifies a Char literal.
    7. charVar = "Z"C

    Sorry to hijack the thread...

  8. #8
    Junior Member
    Join Date
    Nov 2008
    Posts
    20

    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 ;-)

  9. #9
    Addicted Member
    Join Date
    Mar 2008
    Posts
    150

    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
  •  



Click Here to Expand Forum to Full Width