Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

"Find and Replace" Algorithm

Posted on 2001-08-02
9
Medium Priority
?
724 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 53

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
Independent Software Vendors: 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 53

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 150 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…
Suggested Courses

609 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