amol kadam
amol kadam

Reputation: 119

How to split string in group in vb.net

i'm amol kadam,I want to know how to split string in two part.My string is in Time format (12:12).& I want to seperate this in hour & minute format.the datatype for all variables are string. for hour variable used strTimeHr & for minute strTimeMin .I tried below code but their was a exception "Index and length must refer to a location within the string. Parameter name: length"

                If Not (objDS.Tables(0).Rows(0)("TimeOfAccident") Is Nothing Or objDS.Tables(0).Rows(0)("TimeOfAccident") Is System.DBNull.Value) Then
                    strTime = objDS.Tables(0).Rows(0)("TimeOfAccident") 'strTime taking value 12:12
                    index = strTime.IndexOf(":")                        'index taking value 2  
                    lastIndex = strTime.Length                          'Lastindex taking value 5   

                    strTimeHr = strTime.Substring(0, index)               'strTime taking value 12 correctly
     strTimeMin = strTime.Substring(index + 1, lastIndex)   'BUT HERE IS PROBLEM OCCURE strTimeMin Doesn't taking any value
                    Me.NumUpDwHr.Text = strTimeHr
                    Me.NumUpDwMin.Text = strTimeMin
                End If

Upvotes: 1

Views: 3906

Answers (4)

Stephen Wrighton
Stephen Wrighton

Reputation: 37880

The simplest way (assumming that you can ensure the format) is to use a split:

dim strTime as string = objDs.Tables(0).Rows(0).("TimeOfAccident")
dim timeParts() as string =  split(strTime,":")
dim strTimeHr as string = timeParts(0)
dim strTimeMn  as string = timePartS(1)

you'll also want some error handling to check for formatting and that the split generates the array with at least two elements and what not.

EDIT: After looking more closely at your code, I see the cause of your error.

You have this code:

lastIndex = strTime.Length 
strTimeHr = strTime.Substring(0, index)
strTimeMin = strTime.Substring(index + 1, lastIndex)

With that last line giving the error.

The reason that you're getting that error is that strings (and other arrays) in VB.Net are ZERO BASED. Which is why in the strTimeHr field, you're starting at position 0. Length gives you the count, which since the array is zero-based, means the count will be one more than available indexes.

I.e. the last element in a zero based array is the length of the array minus 1.

Therefore, changing your original code to this:

lastIndex = strTime.Length  - 1
strTimeHr = strTime.Substring(0, index)
strTimeMin = strTime.Substring(index + 1, lastIndex)

will work as well.

Upvotes: 1

Thomas
Thomas

Reputation: 64674

Is the datatype of the column "TimeOfAccident" a DateTime? If so, then the simplest solution would be something like:

'Using LINQ to convert the value to a nullable datetime.
Dim timeOfAccident As Nullable(Of DateTime) = dt.Rows(0).Field(Of Nullable(Of DateTime))("TimeOfAccident")

If timeOfAccident.HasValue Then
    Me.NumUpDwHr.Text = timeOfAccident.Hour
    Me.NumUpDwMin.Text = timeOfAccident.Minute
End If

If the column "TimeOfAccident" is a string, then you can do something like:

Dim timeOfAccidentString As String = dt.Rows(0).Field(String)("TimeOfAccident")

If Not String.IsNullOrEmpty(timeOfAccidentString) Then
    Dim accidentTime As DateTime = DateTime.Parse(timeOfAccidentString)
    Me.NumUpDwHr.Text = timeOfAccident.Hour
    Me.NumUpDwMin.Text = timeOfAccident.Minute
End If

Upvotes: 0

Raj Kaimal
Raj Kaimal

Reputation: 8304

string s = "12:13";
DateTime dt = DateTime.Parse(s);

Console.Write(dt.Hour + " " + dt.Minute);

Upvotes: 1

Adriaan Stander
Adriaan Stander

Reputation: 166606

Try something like (String.Split Method )

Dim str As String = "12:13"
Dim strHr = str.Split(":")(0)
Dim strMin = str.Split(":")(1)

Upvotes: 0

Related Questions