CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2006
    Posts
    65

    Extract parameter from string

    Hi,



    I have a string in a text box (text1.text = “chapter-12/heading-321/verse-41”).

    I want to extract the values of chapter, heading and verse as 12,321 and 41 respectively.



    Kindly help.




    Thanks and regards,



    Umar

  2. #2
    Join Date
    Sep 2006
    Posts
    392

    Re: Extract parameter from string

    This my way of getting the numbers
    Code:
    Option Explicit
    Private Sub Command1_Click()
        'Mark A Reference to Microsoft VBScript Regular Expression 5.5 Library
        On Error GoTo Err1
        Dim regx As New RegExp
        Dim m As Match 'to get the eatch match
        Dim mc As MatchCollection   'Contains the match collections
        Dim spattern As String
        regx.Pattern = ("[0-9]+")   'Pattern to get the numerbers only
        regx.Global = True           'Search for global in the text
        If regx.Test(Text1.Text) = False Then MsgBox "No Matches": Exit Sub 'If no matches found then exit sub
        Set mc = regx.Execute(Text1.Text)   'Execute
        Debug.Print mc.Count 'no of occurance
        For Each m In mc    'Loop through each occurance and pring
            Debug.Print m.Value 'print the number values
        Next
    Exit Sub
    Err1:
         MsgBox Err.Description
         Exit Sub
    End Sub
    VS 2005

  3. #3
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Extract parameter from string

    Something like this would work:

    Code:
    sub Form1_Load()
      dim s as integer
      dim arr() as string
      text1.text = “12/321/41”
      arr()=split(text1.text,"/")
      debug.print arr(0)
      debug.print arr(1)
      debug.print arr(2)
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  4. #4
    Join Date
    Sep 2006
    Posts
    635

    Re: Extract parameter from string

    try this,
    I hope it's useful
    Code:
    option explicit
    Sub test()
    Dim d() As String
    Dim s As Variant
    d = Split(Text1.Text, "/")
    For Each s In d
      Debug.Print Right(s, InStr(StrReverse(s), "-") - 1)
    Next
    End Sub

  5. #5
    Join Date
    Oct 2006
    Posts
    65

    Re: Extract parameter from string

    what about following:

    [CODE]
    text1.text = "chapter-1/rukoo-1/verse-1"
    chlen = InStr(Text1.Text, "/rukoo") - (InStr(Text1.Text, "chapter-") + 8)
    rklen = InStr(Text1.Text, "/verse") - (InStr(Text1.Text, "rukoo-") + 6)
    vrlen = InStr(Text1.Text, "verse-") + 6
    ch = Mid(Text1.Text, 9, chlen)
    rk = Mid(Text1.Text, Len(ch) + 16, rklen)
    vr = Mid(Text1.Text, Len(ch) + Len(rk) + 23, vrlen)

    [\CODE]

    Above code solves my problem

    Umar
    Last edited by umararif; March 15th, 2007 at 03:33 AM.

  6. #6
    Join Date
    Oct 2006
    Posts
    327

    Re: Extract parameter from string

    Hello,
    as everyone is showing a method,
    here comes mine, then !


    Code:
    Private Sub Command1_Click()
      toto = "chapter-12/heading-321/verse-41"
      For i = 1 To Len(toto)
        If IsNumeric(Mid(toto, i, 1)) Then
            titi = titi & Mid(toto, i, 1)
        Else
          If titi <> "" And Right(titi, 3) <> " - " Then titi = titi & " - "
        End If
      Next
      MsgBox titi
    End Sub

  7. #7
    Join Date
    Oct 2006
    Posts
    327

    Re: Extract parameter from string

    and a little bit faster one, just for fun ...

    Code:
    Private Sub Command1_Click()
      toto = "chapter-12/heading-321/verse-41"
      While toto <> ""
        If IsNumeric(Mid(toto, 1, 1)) Then
          titi = titi & Val(toto) & " - "
          toto = Mid(toto, 1 + Len(Str(Val(toto))))
        Else
          toto = Mid(toto, 2)
        End If
      Wend
      MsgBox titi
    End Sub

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

Featured