Solved

Richtextbox search

Posted on 2000-04-03
13
176 Views
Last Modified: 2012-08-14
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
Comment
Question by:Geo24
  • 4
  • 3
  • 3
  • +3
13 Comments
 
LVL 12

Accepted Solution

by:
mark2150 earned 100 total points
ID: 2680189
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
 
LVL 6

Expert Comment

by:MELeBlanc
ID: 2680217
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 2680232
<ping>
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Expert Comment

by:TheMask
ID: 2680331
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
 
LVL 12

Expert Comment

by:mark2150
ID: 2680387
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 2680399
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
 
LVL 2

Expert Comment

by:damienm
ID: 2680455
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
 

Expert Comment

by:TheMask
ID: 2681129
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 2681799
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
 

Expert Comment

by:TheMask
ID: 2681970
TheMask changed the proposed answer to a comment
0
 
LVL 12

Expert Comment

by:mark2150
ID: 2682327
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
 

Author Comment

by:Geo24
ID: 2683056
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
 
LVL 12

Expert Comment

by:mark2150
ID: 2683426
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now