Solved

"Find and Replace" Algorithm

Posted on 2001-08-02
9
707 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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!

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

733 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