[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 171
  • Last Modified:

Character based - Search and Replace

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
martalex
Asked:
martalex
  • 2
1 Solution
 
martalexAuthor Commented:
Edited text of question
0
 
RenesCommented:
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
 
yowkeeCommented:
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
 
martalexAuthor Commented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now