Results 1 to 9 of 9

Thread: [2005] Spliting a string

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    [2005] Spliting a string

    hello,

    I have developed some code below to split a string.

    In a string I get back from a database i have for example "13 Hours". I have to split this and put them into 2 different string variables. So it would separate 13 from hours.

    Does anyone know of any better method to do this.

    Many thanks,

    Code:
    'Split the estimated duration into value and periodically (dates) i.e. "4 Days" to "4" "Days"
    Dim duration As String = Me.DsIncidentsControl.IncidentTask(0).EstimatedDuration
    Dim strArray(2) As String 'Array with 2 element to hold the number and date
                    strArray = duration.Split(" ") 'Split at the space
                    Me.txtEstimatedDuration.Text = strArray(0) 'Assign the number value
                    Me.cboDuration.Text = strArray(1) 'Assign the date
    Last edited by steve_rm; May 2nd, 2007 at 06:38 AM.
    steve

  2. #2
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: [2005] Spliting a string

    First off, don't define the array size. Let the Split() function do that.

    Other than that, I don't know of a better way to split strings. That's what the Split function is for, and it's kinda hard to beat...

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: [2005] Spliting a string

    Thanks,

    This is the update

    vb Code:
    1. Dim strArray() As String 'Array with 2 element to hold the number and date
    2.                 strArray = duration.Split(" ") 'Split at the space
    steve

  4. #4
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: [2005] Spliting a string

    Quote Originally Posted by steve_rm
    hello,

    I have developed some code below to split a string.

    In a string I get back from a database i have for example "13 Hours". I have to split this and put them into 2 different string variables. So it would separate 13 from hours.

    Does anyone know of any better method to do this.

    Many thanks,

    Code:
    'Split the estimated duration into value and periodically (dates) i.e. "4 Days" to "4" "Days"
    Dim duration As String = Me.DsIncidentsControl.IncidentTask(0).EstimatedDuration
    Dim strArray(2) As String 'Array with 2 element to hold the number and date
                    strArray = duration.Split(" ") 'Split at the space
                    Me.txtEstimatedDuration.Text = strArray(0) 'Assign the number value
                    Me.cboDuration.Text = strArray(1) 'Assign the date
    well you really can't do this another way but you can change the code.

    VB Code:
    1. 'Split the estimated duration into value and periodically (dates) i.e. "4 Days" to "4" "Days"
    2. Dim duration As String = Me.DsIncidentsControl.IncidentTask(0).EstimatedDuration
    3. 'Dim strArray(1) As String 'Array with 2 element to hold the number and date // Arrays start at 0 so before with strArray(2) you were defining an array with 3 slots.
    4.                 'strArray = duration.Split(" ") 'Split at the space //we can just leave out this step but another way to do it is... Split(duration," ") 'because vb is so friendly :P.
    5.                 'dim strArray() as string = duration.Split(" ") ' another way of splitting into an array.
    6.                 Me.txtEstimatedDuration.Text = duration.Split(" ")(0) 'Assign the number value //instead of defining and then accessing the array we can just split and access right away.
    7.                 Me.cboDuration.Text = duration.Split(" ")(1) 'Assign the date

  5. #5
    Frenzied Member stimbo's Avatar
    Join Date
    Jun 2006
    Location
    UK
    Posts
    1,739

    Re: [2005] Spliting a string

    steve,

    I would also say that in your post 3 it's not entirely correct. You split by a character so you should have:

    vb Code:
    1. Dim strArray() As String = duration.Split(" "c) 'Split at the chr space

    Or
    vb Code:
    1. Dim strArray() As String = duration.Split(CChar(" ")) 'Split at the space
    Stim

    Free VB.NET Book Chapter
    Visual Basic 2005 Cookbook Sample Chapter

  6. #6
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: [2005] Spliting a string

    That's understood by VB.

    Personally, I'm kinda stuck on the VB6 method of doing it:
    vb Code:
    1. Dim strArray() As String = Split(duration, " ")

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: [2005] Spliting a string

    Hello

    What is the difference between:
    vb Code:
    1. Me.txtEstimatedDuration.Text = duration.Split(" "c)(0)
    2. and
    3.                 Me.txtEstimatedDuration.Text = duration.Split(" ")(0)

    I left out the c in the second one. However both worked fine. Just wondering what the difference is.
    steve

  8. #8
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: [2005] Spliting a string

    If you turn Option Strict On then you will see why the c is put there.
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: [2005] Spliting a string

    Thanks Kimmy4,

    I am usually the C# programmer

    Steve
    steve

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