|
-
May 1st, 2007, 11:33 AM
#1
Thread Starter
Frenzied Member
[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
-
May 1st, 2007, 11:39 AM
#2
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...
-
May 1st, 2007, 11:48 AM
#3
Thread Starter
Frenzied Member
Re: [2005] Spliting a string
Thanks,
This is the update
vb Code:
Dim strArray() As String 'Array with 2 element to hold the number and date
strArray = duration.Split(" ") 'Split at the space
-
May 1st, 2007, 11:52 AM
#4
Frenzied Member
Re: [2005] Spliting a string
 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:
'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(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.
'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.
'dim strArray() as string = duration.Split(" ") ' another way of splitting into an array.
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.
Me.cboDuration.Text = duration.Split(" ")(1) 'Assign the date
-
May 1st, 2007, 01:17 PM
#5
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:
Dim strArray() As String = duration.Split(" "c) 'Split at the chr space
Or
vb Code:
Dim strArray() As String = duration.Split(CChar(" ")) 'Split at the space
-
May 1st, 2007, 01:18 PM
#6
Re: [2005] Spliting a string
That's understood by VB.
Personally, I'm kinda stuck on the VB6 method of doing it:
vb Code:
Dim strArray() As String = Split(duration, " ")
-
May 2nd, 2007, 06:40 AM
#7
Thread Starter
Frenzied Member
Re: [2005] Spliting a string
Hello
What is the difference between:
vb Code:
Me.txtEstimatedDuration.Text = duration.Split(" "c)(0)
and
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.
-
May 2nd, 2007, 06:46 AM
#8
Fanatic Member
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 
-
May 2nd, 2007, 06:50 AM
#9
Thread Starter
Frenzied Member
Re: [2005] Spliting a string
Thanks Kimmy4,
I am usually the C# programmer 
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|