Results 1 to 12 of 12

Thread: [RESOLVED] Extract values from string

  1. #1

    Thread Starter
    Fanatic Member snufse's Avatar
    Join Date
    Jul 2004
    Location
    Jupiter, FL
    Posts
    912

    Resolved [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.
    Last edited by snufse; Mar 8th, 2010 at 09:44 AM.

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    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.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Extract values from string

    you can do it with regex:

    vb Code:
    1. Dim testStr As String = "Feed.SmulocV2.Equipment,CAT,,DFT08783,2567,Hours,2010-02-07 06:37:41,Lat:35.10495,Lng:-82.99304"
    2. 'latitude
    3. Dim rx As New Regex("(?<=Lat:)-*\d*\.*\d+")
    4. MsgBox(rx.Match(testStr).Value)
    5. 'longitude
    6. rx = New Regex("(?<=Lng:)-*\d*\.*\d+")
    7. MsgBox(rx.Match(testStr).Value)

    don't forget to import regex:

    vb Code:
    1. Imports System.Text.RegularExpressions

  4. #4

    Thread Starter
    Fanatic Member snufse's Avatar
    Join Date
    Jul 2004
    Location
    Jupiter, FL
    Posts
    912

    Re: Extract values from string

    Worked. Thank you.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Extract values from string

    which code did you use?

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Extract values from string

    Quote Originally Posted by snufse View Post
    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
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  7. #7
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,941

    Re: Extract values from string

    Quote Originally Posted by koolsid View Post
    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

    Sorry if i reply on old post

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [RESOLVED] Extract values from string

    try this:

    vb Code:
    1. Private Function ExtractText(ByVal Source As String, ByVal strOne As String, ByVal strTwo As String) As String
    2.     Dim intRet As Integer = InStr(Source, strOne) + Len(strOne)
    3.     ExtractText = Mid(Source, intRet, InStr(Source, strTwo) - intRet)
    4. End Function

  9. #9
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,941

    Re: [RESOLVED] Extract values from string

    Quote Originally Posted by .paul. View Post
    try this:

    vb Code:
    1. Private Function ExtractText(ByVal Source As String, ByVal strOne As String, ByVal strTwo As String) As String
    2.     Dim intRet As Integer = InStr(Source, strOne) + Len(strOne)
    3.     ExtractText = Mid(Source, intRet, InStr(Source, strTwo) - intRet)
    4. End Function
    Hi friend i need exactly but for vb6b classic

    Sorry if i reply on old post

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [RESOLVED] Extract values from string

    that is for vb6... try it

  11. #11
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,941

    Re: [RESOLVED] Extract values from string

    Quote Originally Posted by .paul. View Post
    that is for vb6... try it
    hummmmmmmmmm.... see the red line

    I use for a test vba for excel
    Attached Images Attached Images  

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [RESOLVED] Extract values from string

    vb Code:
    1. Private Function ExtractText(ByVal Source As String, ByVal strOne As String, ByVal strTwo As String) As String
    2.     Dim intRet As Integer
    3.     intRet = InStr(Source, strOne) + Len(strOne)
    4.     ExtractText = Mid(Source, intRet, InStr(Source, strTwo) - intRet)
    5. End Function

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