How can u search a string if it contains a certain text I specify!
Printable View
How can u search a string if it contains a certain text I specify!
Try InStr.
Use Instr
Code:InStr(1, Tosearchtext, "lookingfortext") ' 1 is the starting postion
][P
can u explain it's parameters like [start] and string1 and string2 and stuff like that, what do they mean
ok
string1 - string to look in
string2 - string to search for
start - character to begin searching from (1 = first char)
Use InStr function
InStr([start_pos],str_to_search,search_for [,compare_type])
Ex:
Dim x as Integer
Dim strToSrch as String
strToSrch = "This is so easy..."
x = InStr(strToSrch, "easy")
If string was found the value of the x will be stating position of the string (in this case x=12).
If string was not found then x=0
OK, what if I want it to return a word from a specific line!
You can't as such, since a logical line is terminated by a newline, not where it wraps on-screen. Try splitting the lines up, and searching individually.
This is what I mean!
I have lets say 4 lines:
hello there.
My Name is VIP3R.
I'm making a program!
This program is not working.
and i want my program to return VIP3R from the second line!
This might be something you need:
You'll find VIP3R by:Code:Function RetLineWord(ByVal str As String, linenum As Long, wordnum As Long) As String
Dim x&, pos&, pos2&
For x = 2 To linenum
pos = InStr(pos + 1, str, vbCrLf)
pos = pos + 1
Next x
For x = 2 To wordnum
pos = InStr(pos + 1, str, " ")
Next x
pos2 = InStr(pos + 1, str, " ")
If pos2 = 0 Then pos2 = Len(str) + 1
RetLineWord = Mid(str, pos + 1, pos2 - pos - 1)
End Function
msgbox RetLineWord(yourtext,2,4)
Is there a better code. Also I have two frames in on one page and I want to get the html of one of them! How do I do that?
Try your code with a string with over 20 lines or am html page, it's not working properly. Can u please reply soon!
Hmm, dunno it works well but it's a bit slow for huge texts, i did an emulation:
What error did you get? which line?Code:Sub main()
For n = 1 To 105
For s = 1 To n / 2
d = d & "TEXT" & n & "," & s & " "
Next s
d = d & vbCrLf
Next n
Debug.Print RetLineWord(d, 100, 50)
End Sub
Function RetLineWord(ByVal str As String, linenum As Long, wordnum As Long) As String
Dim x&, pos&, pos2&
For x = 2 To linenum
pos = InStr(pos + 1, str, vbCrLf)
pos = pos + 1
Next x
For x = 2 To wordnum
pos = InStr(pos + 1, str, " ")
Next x
pos2 = InStr(pos + 1, str, " ")
If pos2 = 0 Then pos2 = Len(str) + 1
RetLineWord = Mid(str, pos + 1, pos2 - pos - 1)
End Function
When my text has spaces in between the lines, then the program stops right before the space! For ex:
Hi
This is VIP3R
I skipped One Line
It stop on "This is VIP3R"
It stops on the second line because there's a double space! Try it your self!