Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find String

Status
Not open for further replies.

bebig

Technical User
Oct 21, 2004
111
0
0
US
Hi,

I am searching specific string.
String looks like
------
date -s "03/01/2005 12:13:03"Tue Mar 1 12:13:03 PST 2005bash#
------
To get string,

FTPUpload.Text1.SelText = Chr$(CH)

To get string, it takes one by one on FTPUpload.Text1.Text.

For example,
d
a
t
e
-s
.......
b
a
s
h
#

So, final string looks like As above.

If I only want to get Tue Mar 1 12:13:03 PST 2005, how can I do??

Would you please help me??

Thank you in advance.



 
Look up the Instr and Mid$ functions, or use Split

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
can I use those ways when a string take one by one?
 
No but you can concatenate your individual characters if you like.

Otherwise process each individual character, starting when you see Chr$(34) and then counting characters

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
Well, I tried it..but I could not get it.
I am not sure how to take each character as a whole string.
When I check textbox, it still gets one by one.

If I take it as a whole string, I can use MID function to get exact string.

Would you please tell me how to get a whole string, not each character?

Thank you in advance.

 
bebig,
There is a certain amount of simple math that is involved whenever I want to find a string inside of a string. As Johnwm mentions, the inStr and mid functions would be two functions which would really get you going. Since the InStr function gives you the location to a single characher within your string as an integer, then what you have to do is figure out what charachters precede and procede the string you wish to grab. Once you have this knowledge you can then use the Mid function to set the starting position and the ending position of your treasured string. For instance...

Option Explicit

Function GetString(FirstChar As String, LastChar As String, TexttoSearch As String) As String
On Error GoTo GetString_err
Dim Startint As String
Dim Endint As String
Dim TmpString As String

Startint = InStr(1, TexttoSearch, FirstChar, vbTextCompare)
Endint = InStr(Startint, TexttoSearch, LastChar, vbTextCompare)
TmpString = Mid(TexttoSearch, Startint, Endint - Startint)
GetString = TmpString
Exit Function

GetString_err:
MsgBox "Error Number: " & Err.Number & vbCrLf & _
"Error Source: " & Err.Source & vbCrLf & _
"Error Description: " & Err.Description & vbCrLf, vbCritical, "Getstring Error"
Err.Clear
End Function

If you put this into a module then you will be able to search any string for a specific string. As you can see, the first InStr function locates the first instance of your starting search term, and the second instance of Instr locates the closest instance of the second search term. Since these you now have the two locations, you simply use the Mid function and a little subtraction to figure out what is between the two numbers. Note: this code does not have provisions in the event that the string is not found. This would be easy to insert, but I just didn't do it. Oops!

Ok, well, Now that you know the just of these two functions, you can now do all sorts of fun things with it; let your imagination run wild!!!

LF




"As far as the laws of mathematics refer to reality, they are not certain; as far as they are certain, they do not refer to reality."--Albert Einstein
 
Here's a Regular Expression version of Homersim's function. Avoids all that nasty maths ...
Code:
[blue]Function GetString(FirstChar As String, LastChar As String, TexttoSearch As String) As String
    Dim result As Object
    With CreateObject("vbscript.regexp")
        .Global = True
        .MultiLine = True
        .Pattern = FirstChar & ".*?" & LastChar
        Set result = .Execute(TexttoSearch)
    End With
    If result.Count > 0 Then GetString = result(0)
End Function[/blue]
 
Interesting...




Very Interesting...

Where do you get this stuff??? :)

LF

"As far as the laws of mathematics refer to reality, they are not certain; as far as they are certain, they do not refer to reality."--Albert Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top