Results 1 to 9 of 9

Thread: Split string

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2017
    Posts
    147

    Split string

    hi i am trying to split a string which consists of

    Code:
    HI:bye:yes:no
    but i only want to split the string at the first instance of ":"

    Code:
    string  = split("HI:bye:yes:no",":")(0) ' Gives me "Hi"
    i want Split(1) to contain the rest of the string

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Split string

    Maybe

    Code:
            Dim s As String = "HI:bye:yes:no"
            Dim p() As String = s.Split(New Char() {":"c}, 2)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Split string

    Instead of Split, you could just use IndexOf and Substring. IndexOf gives you the index of the first instance of whatever you pass it, while SubString returns a substring (as you might guess). Trying to force Split to do it is fine, but a waste of time. Strings can't be changed, so when you call Split, you aren't changing the string, you are just creating an array of new strings. You'd have to take the first element from the array, then combine the other elements back together. IndexOf and SubString would be more direct.
    My usual boring signature: Nothing

  4. #4
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Split string

    I'm agree with Shaggy Hiker

    Here is the way to do
    vb.net Code:
    1. Dim s As String = "HI:bye:yes:no"
    2.         Dim i As Integer = s.IndexOf(":"c)
    3.         Dim s1 As String
    4.         Dim s2 As String
    5.         If i >= 0 Then
    6.             s1 = s.Substring(0, i)
    7.             s2 = s.Substring(i + 1)
    8.         End If



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

    Re: Split string

    Quote Originally Posted by Shaggy Hiker View Post
    You'd have to take the first element from the array, then combine the other elements back together. IndexOf and SubString would be more direct.
    Actually, that's not true. A number of the overloads of String.Split (which should be used in preference to Split) let you specify a maximum number of parts. If you only want to split on the first occurrence of a character then specify the max number of parts as 2:
    vb.net Code:
    1. Dim str = "HI:bye:yes:no"
    2. Dim arr = str.Split(":"c, 2)

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Split string

    I sit corrected. I've never seen that overload.

    All I'll say is that it's a darn good thing that cats have nine lives, considering how many ways programmers have to skin them.
    My usual boring signature: Nothing

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: Split string

    Quote Originally Posted by dbasnett View Post
    Maybe

    Code:
            Dim s As String = "HI:bye:yes:no"
            Dim p() As String = s.Split(New Char() {":"c}, 2)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Split string

    @dbasnett, and you managed to get the syntax right, unlike me.

  9. #9
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Split string

    You almost had it right ... try:
    Code:
    split("HI:bye:yes:no",":", 2)
    Kris

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