Solved

"Find and Replace" Algorithm

Posted on 2001-08-02
9
706 Views
Last Modified: 2008-02-01
Dear all,

I am writing an edito. Can anyone of you  tell me where to find the algorithm how to write functionality of "Find and Replace" for an editor.

Best Regards,

MGlobal.
0
Comment
Question by:MGlobal
9 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6347541
Using the InStr() function, you should be able to do your job...
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 6347562
Hi,

Use the Replace function in VB6
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6347567
Use replace function to replace string occurences. Try the below piece of code.

dim str1 as string

str1 = "Hello World"
replace(str1, "Hello", "hehe")
msgbox str1

hongjun
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 33

Expert Comment

by:hongjun
ID: 6347569
ryancys, I did not see your code when I type mine. It is just less than a minute difference.

For your information, replace function is not in VB5.

hongjun
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 6347618
Hi hungjun,

It's ok :)
0
 
LVL 33

Expert Comment

by:hongjun
ID: 6347624
:)
0
 

Accepted Solution

by:
CTrip earned 50 total points
ID: 6347787
1.create a Form and called it: "frmMain"
  put in the form RichTextBox1
  (RichTextBox1 will contain many words)
  create a button "find_and_replace" with that code:
    frmSearch.Show , Me


2.create a Form with the name: "frmSearch"
  put in the form: text1, Command1
  (text1 will contain the word to search)
  "frmSearch" code:

Public Sub Command1_Click()
'Dim text1.text
Dim pos As Integer
    pos = frmMain.RichTextBox1.SelStart
    If chkCaseSensitive.Value = vbChecked Then
        pos = InStr(pos + 1, frmMain.RichTextBox1.Text, txtSearch.Text)
    Else
        pos = InStr(pos + 1, frmMain.RichTextBox1.Text, txtSearch.Text)
    End If
    If pos = 0 Then
        frmMain.RichTextBox1.SelStart = 0
        frmMain.RichTextBox1.SelLength = 0
    Else
        frmMain.RichTextBox1.SelStart = pos - 1
        frmMain.RichTextBox1.SelLength = Len(txtSearch.Text)
    End If
   
txtSearch.Text = Text1.Text
rsButton5_Click
End Sub
Private Sub rsButton5_Click()
Dim pos As Integer

    pos = frmMain.RichTextBox1.SelStart
    pos = InStr(pos + 2, frmMain.RichTextBox1.Text, txtSearch.Text)
    If pos = 0 Then
        frmMain.RichTextBox1.SelStart = 0
        frmMain.RichTextBox1.SelLength = 0
    Else
        frmMain.RichTextBox1.SelStart = pos - 1
        frmMain.RichTextBox1.SelLength = Len(txtSearch.Text)

    'Replace The Word ----------->
    'TO REPLACE THE WORD WITH ANOTHER WORD
    'frmMain.RichTextBox1.SelText = "WORD2"
   
    End If
  If frmMain.RichTextBox1.Visible = True Then
txtSearch.Text = frmMain.RichTextBox1.SelText
End If
End Sub
0
 
LVL 3

Expert Comment

by:andysalih
ID: 6353817
reject the proposed answer if you want more feed from EE users

o by the way nealy forgot

this will do the trick for you

Use this function to do a search and replace function. It works by finding the next instance of the string that you are looking for, and gets everything to the left and right of it, inserting the new string between the two parts.

Public Function replaceall(searchstring As String, _
findstring As String, replacestring As String) As String

Dim curpos As Long

curpos = 1

Do

curpos = InStr(curpos, searchstring, findstring)
searchstring = Left$(searchstring, curpos - 1) & _
replacestring & Right$(searchstring, Len(searchstring) _
- curpos - Len(findstring) + 1)

Loop Until InStr(searchstring, findstring) = 0

replaceall = searchstring

End Function
You could easily modify this function to search only a certain range of the string, if you needed


cheers
Andy
0
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 6375298
Have a look at:
http://www.freevbcode.com/ShowCode.Asp?ID=1902
(Find And Replace for Edit Controls)

Easy and simple.

Hope this helps.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…

828 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