Solved

Character based - Search and Replace

Posted on 1998-07-02
4
166 Views
Last Modified: 2010-05-03
Hi there!

I trying to do a software which one will read a text file and replace a couple of characters by some others.

Presently I'm using the RichTextBox control to open my file, it's possible to use Find with the RichTextBox, but because I got probably 20 different characters to search and replace I would prefer read each character one by one and look if it's not one that I would like to convert.

My problem is : how can I scan character by character from the content of RichTextBox.text?

Ex: Replace every "o by x" and every "e by q"

 Source : Would you likq to be notifiqd about this quqstion?
    Target : Wxuld yxu like tx be nxtified abxut this questixn?

Thanks
0
Comment
Question by:martalex
  • 2
4 Comments
 

Author Comment

by:martalex
ID: 1464888
Edited text of question
0
 

Accepted Solution

by:
Renes earned 50 total points
ID: 1464889
Charpos as integer
Textline as string
Read a line of the richtextbox into the string textline (should be easy)
Than look for the char (example X)

Charpos =1 (begin of line)
Find the character with:
 Do While char <> "X"
          Charpos = Charpos + 1
          char = Mid(textline, Charpos, 1)
 Loop

Use the selstart and sellength properties of the richtxtbox to select the character

Selstart will be charpos and sellength will be 1 (because you're replacing one character


Hope this helps, comment me if you have questions..
0
 
LVL 4

Expert Comment

by:yowkee
ID: 1464890
martalex,

  Use this fast 'find and replace' function:

-----
Public Function FastReplace(SSrch$, SFind$, SRepl$) As String

    Dim Src() As Byte, Dst() As Byte, R() As Byte, F() As Byte
    Dim LenF&, LenR&, LenDst&, i&, j&, OutPos&

    Const ChunkSize& = 4096
    If SSrch = "" Or SFind = "" Then Exit Function
    Src = SSrch: F = SFind: R = SRepl
    LenF = UBound(F): LenR = UBound(R)
    LenDst = ChunkSize: ReDim Dst(0 To LenDst - 1)
 
    For i = 0 To UBound(Src) Step 2
        For j = 0 To LenF Step 2
            If Src(i + j) <> F(j) Then Exit For
        Next j
        If j > LenF Then 'Found
            For j = 0 To LenR Step 2
                If OutPos >= LenDst Then
                    LenDst = LenDst + ChunkSize
                    ReDim Preserve Dst(0 To LenDst)
                End If
                Dst(OutPos) = R(j): OutPos = OutPos + 2
            Next j

            i = i + LenF - 1
        Else
            If OutPos >= LenDst Then
                LenDst = LenDst + ChunkSize
                ReDim Preserve Dst(0 To LenDst)
            End If

            Dst(OutPos) = Src(i): OutPos = OutPos + 2
        End If
    Next i
 
    ReDim Preserve Dst(0 To OutPos - 2): SSrch = Dst
    FastReplace = SSrch$
   
End Function

' Example ---
Private Sub Command1_Click()
   Dim sResult As String

   sResult=FastReplace("Would you","o","s")
   ' sResult ---> "Wsuld ysu"  

End Sub
-----

Regards


0
 

Author Comment

by:martalex
ID: 1464891
Hi yowkee!  You solution is really cool!! Complex, but I like it.  I'm a little bit rusty in programmation so, it's kind of very difficult to follow your code but thanks for you help!

I hope one day to be able to understand you code...

Alex
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…

860 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