[RESOLVED] Extract values from string
Have following string:
PHP Code:
Feed.SmulocV2.Equipment,CAT,,DFT08783,2567,Hours,2010-02-07 06:37:41,Lat:35.10495,Lng:-82.99304
The string will always have text "Lat:" and "Lng:". The value of Lat and Lng could in some cases be nothing.
Now I need to extract the values for Lat and Lng. Any good way of doing this? Thank you.
Re: Extract values from string
Untested:
Code:
'~~> This function will give you the value of Lat
'~~> strOne = "Lat:"
'~~> strTwo = ",Lng:"
Private Function ExtractText(ByVal Source As String, _
ByVal strOne As String, ByVal strTwo As String) As String
Dim lngRet As long = Source.IndexOf(strOne) + strOne.Length
Return Source.Substring(lngRet, Source.IndexOf(strTwo) - lngRet)
End Function
Similarly you can find the position of "Lng:-" and get the text after that.
Re: Extract values from string
you can do it with regex:
vb Code:
Dim testStr As String = "Feed.SmulocV2.Equipment,CAT,,DFT08783,2567,Hours,2010-02-07 06:37:41,Lat:35.10495,Lng:-82.99304"
'latitude
Dim rx As New Regex("(?<=Lat:)-*\d*\.*\d+")
MsgBox(rx.Match(testStr).Value)
'longitude
rx = New Regex("(?<=Lng:)-*\d*\.*\d+")
MsgBox(rx.Match(testStr).Value)
don't forget to import regex:
vb Code:
Imports System.Text.RegularExpressions
Re: Extract values from string
Re: Extract values from string
Re: Extract values from string
Quote:
Originally Posted by
snufse
OK, I get the correct value for Lat. Msgbox shows nothing for Lng.
Yes like I mentioned that function is for Lat only. You will have to write a different piece of code for Lan...
Give it a try... if you get stuck then post the code that you have tried and we will definitely help you :)
Re: Extract values from string
Quote:
Originally Posted by
koolsid
Untested:
Code:
'~~> This function will give you the value of Lat
'~~> strOne = "Lat:"
'~~> strTwo = ",Lng:"
Private Function ExtractText(ByVal Source As String, _
ByVal strOne As String, ByVal strTwo As String) As String
Dim lngRet As long = Source.IndexOf(strOne) + strOne.Length
Return Source.Substring(lngRet, Source.IndexOf(strTwo) - lngRet)
End Function
Similarly you can find the position of "Lng:-" and get the text after that.
Hi friend i need exactly but for vb6b classic:p:rolleyes:
Sorry if i reply on old post:afrog::bigyello:
Re: [RESOLVED] Extract values from string
try this:
vb Code:
Private Function ExtractText(ByVal Source As String, ByVal strOne As String, ByVal strTwo As String) As String
Dim intRet As Integer = InStr(Source, strOne) + Len(strOne)
ExtractText = Mid(Source, intRet, InStr(Source, strTwo) - intRet)
End Function
Re: [RESOLVED] Extract values from string
Quote:
Originally Posted by
.paul.
try this:
vb Code:
Private Function ExtractText(ByVal Source As String, ByVal strOne As String, ByVal strTwo As String) As String
Dim intRet As Integer = InStr(Source, strOne) + Len(strOne)
ExtractText = Mid(Source, intRet, InStr(Source, strTwo) - intRet)
End Function
Hi friend i need exactly but for vb6b classic
Sorry if i reply on old post
Re: [RESOLVED] Extract values from string
that is for vb6... try it
1 Attachment(s)
Re: [RESOLVED] Extract values from string
Quote:
Originally Posted by
.paul.
that is for vb6... try it
hummmmmmmmmm.... see the red line:wave:
I use for a test vba for excel
Re: [RESOLVED] Extract values from string
vb Code:
Private Function ExtractText(ByVal Source As String, ByVal strOne As String, ByVal strTwo As String) As String
Dim intRet As Integer
intRet = InStr(Source, strOne) + Len(strOne)
ExtractText = Mid(Source, intRet, InStr(Source, strTwo) - intRet)
End Function