Solved

Character based - Search and Replace

Posted on 1998-07-02
4
163 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

762 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