Find And Replace String

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pa2ratczi
    New Member
    • Mar 2007
    • 19

    Find And Replace String

    Hi goo day! I'am Darrel, a newbie with visual basic, i have a problem with my recent project.

    the projecti have is to replace the text from one letter to another. I dont know how to code it.

    I'am using text box for my (input), another textbox for the letter to be replace, another text box for the letter to be place. and display the whole text in a label..

    can some give me some hint or sample codes. Thank you so much!!!

    Hoping for an immediate reponse for it!!!
  • samycbe
    New Member
    • Feb 2007
    • 83

    #2
    Dear Friend, Try this.......

    1. I have one text box named TxtA and command button CmdProcess
    2. I want to replace the text "a" with "z" in TxtA on cmdProcess - click
    3. Please look the code on cmdProcess_clic k
    4. Instead of "a" you can use other textbox.text

    Code:
    Dim a As String
    a = TxtA.Text
    a = ""
    Dim x As String
    x = ""
    For i = 1 To Len(TxtA)
        If Mid(TxtA, i, 1) = "a" Then
            x = x + "b"
        Else
            x = x + Mid(TxtA, i, 1)
        End If
    Next
    MsgBox x
    give a feed back

    samy
    Last edited by Killer42; Mar 6 '07, 05:26 AM. Reason: Please use [CODE]...[/CODE] tags around your code

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Actually, while the details may depend on what version of VB you are using, in VB6 you can use the Replace() function to do it in one statement.

      Comment

      • pa2ratczi
        New Member
        • Mar 2007
        • 19

        #4
        that's not the output that i have to achieve, the program is like this

        the user must enter a word in a textbox1(of any length) and must enter/assign a letter in textbox2 which is the letter to be replace from textbox1 and the user must enter a letter in textbox3 which is the letter to be substituted. and display the whole text in a label.

        its like this if the user enters the word "paragraph" and the user wanted to change all letter "a" with letter "b" and the result must be "pbrbgrbph" .

        hope you can help me sir.... its very difficult for me.... thank you

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          That's what the code did, that samycbe posted. It's just that some of it was hard-coded rather than being taken from a textbox. After all, you can't expect us to do everything for you - you need to make a serious attempt at the work yourself.

          Anyway, like I said, check out the Replace function. It's in the manual, and will do it all in one statement.

          Comment

          • vijaydiwakar
            Contributor
            • Feb 2007
            • 579

            #6
            Originally posted by pa2ratczi
            that's not the output that i have to achieve, the program is like this

            the user must enter a word in a textbox1(of any length) and must enter/assign a letter in textbox2 which is the letter to be replace from textbox1 and the user must enter a letter in textbox3 which is the letter to be substituted. and display the whole text in a label.

            its like this if the user enters the word "paragraph" and the user wanted to change all letter "a" with letter "b" and the result must be "pbrbgrbph" .

            hope you can help me sir.... its very difficult for me.... thank you
            try this code
            replace("<thy string>","<repl ace char>","<replac ed by>")
            lbl.caption=rep lace(txt1.text, txt2.text,txt3. text)

            Comment

            • pa2ratczi
              New Member
              • Mar 2007
              • 19

              #7
              yes i know that, that's why ive tried using mid.

              the code its like this

              Code:
              Private Sub Command1_Click()
              Dim str1 As String
              Dim str2 As String
              Dim str3 As String
              
              
              str1 = Text1.Text
              str2 = Text2.Text
              str3 = Text3.Text
              
              If Mid(str1, 1, 1) = str2 Then
                  Mid(str1, 1, 1) = str3
                  Label4.Caption = Mid(str1, 1)
              
              End If
              End Sub
              and its working...but the problem is wat if the text is to long, i dont know how to loop my condition..
              Last edited by Killer42; Mar 6 '07, 07:43 AM. Reason: Please use CODE tags as appropriate

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Well, you could use a For loop with the length of the original string as the upper limit.

                Also, when posting code, please use appropriate tags around it for readability. Details can be found here.

                Comment

                • pa2ratczi
                  New Member
                  • Mar 2007
                  • 19

                  #9
                  can help me how to construct a loop using "FOR".

                  Comment

                  • pa2ratczi
                    New Member
                    • Mar 2007
                    • 19

                    #10
                    guys, ive done it am finished with my project, thanks a lot for all the help, to contribute here the code....

                    Code:
                    Private Sub Command1_Click()
                    Dim str1 As String
                    Dim str2 As String
                    Dim str3 As String
                    Dim Counter As Integer
                    
                    
                    str1 = Text1.Text
                    str2 = Text2.Text
                    str3 = Text3.Text
                    
                    For i = 1 To Len(str3)
                        Counter = i + 1
                        If Mid$(str3, i, 1) = str2 Then
                            Mid$(str3, i, 1) = str1
                            Label4.Caption = Mid$(str3, 1)
                        End If
                    Next
                    
                    End Sub
                    Hope you'll still help me guys sooner or later.. thanks!
                    Last edited by Killer42; Mar 6 '07, 09:35 PM. Reason: Please remember to use [CODE] tags!

                    Comment

                    • Lavs
                      New Member
                      • Mar 2007
                      • 16

                      #11
                      Originally posted by pa2ratczi
                      that's not the output that i have to achieve, the program is like this

                      the user must enter a word in a textbox1(of any length) and must enter/assign a letter in textbox2 which is the letter to be replace from textbox1 and the user must enter a letter in textbox3 which is the letter to be substituted. and display the whole text in a label.

                      its like this if the user enters the word "paragraph" and the user wanted to change all letter "a" with letter "b" and the result must be "pbrbgrbph" .

                      hope you can help me sir.... its very difficult for me.... thank you

                      Halu there... this is my suggestion...

                      'You should have three textboxes, a label, and one command button...
                      'Use the Replace() function in visual basic

                      Dim OriginalString, Replacement, tobeReplaced As String
                      Private Sub Command1_Click( )
                      OriginalString = Trim$(Text1.Tex t)
                      Replacement = Trim$(Text2.Tex t)
                      tobeReplaced = Trim$(Text3.Tex t)
                      Label1.Caption = Replace(Origina lString, tobeReplaced, Replacement)
                      End Sub

                      Comment

                      Working...