Solved

"Find and Replace" Algorithm

Posted on 2001-08-02
9
697 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 49

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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 49

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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…
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…
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…

757 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

21 Experts available now in Live!

Get 1:1 Help Now