Solved

Character based - Search and Replace

Posted on 1998-07-02
4
168 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
[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
  • 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…

705 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