Solved

Character based - Search and Replace

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Write data from SQL to CSV file - position 1 53
VBA open file from excel cell 4 36
clicking a shape in a frame array vb6 3 40
Advice in Xamarin 21 57
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 …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…

863 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

27 Experts available now in Live!

Get 1:1 Help Now