Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

Richtextbox search

I want a function that searches the word in between two tags like <font>red</font> the fastest way possible in vb. You should be able to call it like this,,

dim str1,str2 as string
str1="<fon>"
str2="</font>"
call SearchBetween(RTF1,str1,str2,output)
rtf2.text=output

the string should be able to be anything like.. blablabla and hello

And again..as fast as possible
0
Geo24
Asked:
Geo24
  • 4
  • 3
  • 3
  • +3
1 Solution
 
mark2150Commented:
public function isolate(rtb as string, str1 as string, str2 as string) as string
marker1 = instr(1, rtb, str1, vbTextCompare)
if marker1 < 1 then exit function
marker2 = instr(marker1, rtb, str2, vbtextCompare)
if marker2 < 1 then exit function
isolate = mid( rtb, marker1 + len(str1) + 1, str2-marker1 + len(str1) -1)
end function

This should be close for off the top of my head.

M

0
 
MELeBlancCommented:
If there might be multiple instances of the string you are searching for (and you are usign VB6) then you could use the Filter function.
Otherwise, you could use the InStr function.
0
 
caraf_gCommented:
<ping>
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
TheMaskCommented:
Hi, this code is your straight forward solution,
Note: if you didn't put either the StartTag or EndTag the code returns ""

Private Sub SearchBetween(ByVal strInput As String, ByVal strStartTag As String, ByVal strEndTag As String, strOutput As String)
Dim StartTagPosition As Long, EndTagPosition As Long
StartTagPosition = InStr(1, strInput, strStartTag, vbTextCompare)
StartTagPosition = StartTagPosition + Len(strStartTag) - 1
EndTagPosition = InStr(1, strInput, strEndTag, vbTextCompare)
If (StartTagPosition And EndTagPosition) = 0 Then strOutput = "": Exit Sub
strOutput = Mid(strInput, StartTagPosition + 1, EndTagPosition - StartTagPosition - 1)
End Sub
0
 
mark2150Commented:
Hey TheMask!

Your answer is almost identical to my comment!

Tsk, tsk, tsk! It's considered "poor form" to post an ANSWER that is the same or nearly so to a previously posted COMMENT!

M
0
 
caraf_gCommented:
mark2150,

Yes, I agree. But TheMask is quite new (first login 25 March 2000) so give TheMask a chance...

TheMask, you should really check previously posted comments and answers before you post your own suggested answer. It is commonly accepted practice here, out of politeness, not to lock the question with an answer if it is very similar to something posted earlier.

You can however withdraw your answer. Would you kindly do so?
0
 
damienmCommented:
Private Function SearchBetween(rtb As String, tagStart As String, tagEnd As String, schval As String)
On Error Resume Next
Dim tmpVal As String

startVal = 1
tmpVal = Mid(rtb, InStr(1, rtb, tagStart) + Len(tagStart), InStr(1, rtb, tagEnd) - InStr(1, rtb, tagStart) - Len(tagStart))
If InStr(tmpVal, schval) Then
    MsgBox tmpVal
End If
End Function
0
 
TheMaskCommented:
To mark2150, caraf_g and all:
Sorry guys for the unpolite behaviour,i didn't think of that point
but i thought that as long as i am sure of my answer i have to make it an answer.

To Geo24:  don't give me the points if you i did what you want cuz mark2150 did the same and he was quicker too

:)

0
 
caraf_gCommented:
Hi TheMask, thanks for coming back on this. You don't have to ask Geo24 to reject the answer. If you look at the thread, you'll see you have the option to withdraw your answer. Anyway - again, welcome to EE. Hope you'll enjoy it as much as we do!
0
 
TheMaskCommented:
TheMask changed the proposed answer to a comment
0
 
mark2150Commented:
TheMask,

Welcome to E-E. It's not really about the points - it's about helping. But we are a prickley bunch and we've developed some ground rules.

Myself, I essentially NEVER post "answers", it shuts down the dialog on the Q. If the poster likes my response they use the "Accept Comment as Answer" feature and award the points that way.

Always read the threads. Not only do you see what is and what isn't already in there, but you can pick up some good tips as well.

In fact even tho I'm a top expert, hardly a day goes by that I don't learn a new trick. Just today I found out something about the DIR command that I didn't know! I was in the process of telling someone how their post was wrong when I read it closely, saw something I'd not noticed, checked with the help and discovered that the "bad" code I was about to pan was faster and more efficient than my "good" code! (Erick37!)

Another tip - don't answer Q's that are obvously "homework". Q's that start out "Describe in detail..." or "Contrast and compare..." or "Create a program that..." We are not a homework service and people that post that kind of Q usually get howled down.

E-E is a strong supporter of copyright laws and feel free to report anyone asking how to beat a copy protection or licensing scheme.

Anyway, E-E is a fun place to hang out. We have a lot of good technical stuff and some great people. Be sure to check out the Lounge if you need a break from "geek speak".

Again, welcome!

M
0
 
Geo24Author Commented:
But what if there is for example a " and " on each side, then it dont return nothing. Like, color="red" and i want red. Otherwise its great, thanx! And thanx to all others to.
0
 
mark2150Commented:
Simple enough.

const quote as string * 1 = """"

str1 = "color=" & quote
str2 = quote

This will find the word *red* in the middle of color="red"

M
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now