-
March 15th, 2007, 01:30 AM
#1
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
-
March 15th, 2007, 02:04 AM
#2
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
-
March 15th, 2007, 02:07 AM
#3
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
-
March 15th, 2007, 02:09 AM
#4
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
-
March 15th, 2007, 03:31 AM
#5
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.
-
March 15th, 2007, 05:09 AM
#6
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
-
March 15th, 2007, 05:25 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|