Solved

"Find and Replace" Algorithm

Posted on 2001-08-02
9
703 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 142

Expert Comment

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

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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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 50

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

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

Title # Comments Views Activity
Access 2013 combo box not working 3 43
VB6 - Compare and highlight cell not the same 3 49
Exit a vb6 apps when a calling it apps closes 15 46
Help me. 3 54
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

815 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

10 Experts available now in Live!

Get 1:1 Help Now