Replace Function

Hello, i'm new here...

i need a simple script for a class i teach. the script has to do with the replace function.

it needs to have a textfield (id = TEXTBOX) and a button (id = REPLACE). when the button is clicked, any text inside the textbox that was typed by the user will be replaced. for example, everytime '1' shows up, it will be replaced by '2'. I will need to have about 26 diffrent replace criteria. if someone could write me a script, or direct me towards one, i'd appreicate it.

thanks in advance!!

(by the way, the script needs to be for VB6)
Who is Participating?
Valliappan ANSenior Tech ConsultantCommented:
Sorry about that. Please try this:
    Dim myarray(1 To 26, 1 To 2)
    Dim i As Integer
    'Put your search and replace strings here.
    myarray(1, 1) = "a"        'first search string
    myarray(1, 2) = "A"        'first replace string
    myarray(2, 1) = "bc"     'second search string
    myarray(2, 2) = "BB"     'second replace string and so on.
    myarray(3, 1) = "de"
    myarray(3, 2) = "FF"
   'do all 26 replacements.
    For i = LBound(myarr, 1) To UBound(myarr, 1)
        TxtReplace.Text = Replace(TxtReplace.Text, myarray(i, 1), myarray(i, 2))
Hope it helps.
Dim orig As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Dim repl As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz9876543210"

' simple, change capitals to lower case and visa versa, and for numbers it's 9 minus the original number
' with this you can completely customise which letter changes to what.

Private Sub Replace_Click()

    Dim str As String, newStr As String

    str = Texbox.Text
    For i = 0 To Len(str) - 1

        pos = InStr(orig, Mid(str, i , 1))
        If pos > -1 Then newStr = newStr & Mid(repl, pos, 1)


    Textbox.Text = newStr

End Sub

the other way is...

Private Sub Replace_Click()

    For i = 0 To Len(orig) - 1

        Textbox.Text = Replace(Textbox.Text, Mid(orig, i, 1), Mid(repl, i, 1))


End Sub

In this case if the string inside the text box is generally larger than 62 characters then the second method will be faster. otherwise the first one is best.

hope this helps

P.S.    IT'S CODE     NOT SCRIPT     GET IT RIGHT   (if it compiles it's code, if it's script it's interpreted)
you might want to change the if so that if the letter is not found in the string 'orig' that the original character remains (as would happen in the second method

 If pos > -1 Then newStr = newStr & Mid(repl, pos, 1)

would become

If pos > -1 Then
    newStr = newStr & Mid(repl, pos, 1)
    newStr = newStr & Mid(str, i, 1)
End If
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

whose_da_n00bAuthor Commented:
ok, but what if i want to replace whole words, ie "WORD" with "SENTENCE"?
Valliappan ANSenior Tech ConsultantCommented:
You need to use like monkesdb's code, but with an 2-d array with 26 elements. Like this;
Dim MyArray(1 To 26, 1 To 2)
Dim i As Integer


for i = lbound(MyArray) To ubound(MyArray)
      Replace(TxtReplace.Text, MyArray(1,i), MyArray(2,i)

Hope it helps.
whose_da_n00bAuthor Commented:
btw it dosent work- "Dim orig As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Dim repl As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz9876543210"" creates an error... HELP...
whose_da_n00bAuthor Commented:
valli... i tried yours... i get an error.

"Replace(TxtReplace.Text, MyArray(1,i), MyArray(2,i)" gives me an invalid use of property error...

*note: raised teh points up a bit*
Valliappan ANSenior Tech ConsultantCommented:
You need to create a text box name "TxtReplace" and may be a command button. In the command button's click event, you might need to put the above code.
Option Explicit
Dim inti As Integer
Dim strReplaceString As String
Private Sub Command1_Click()
    strReplaceString = Text1.Text
    For inti = 1 To 2
        Call FindReplace(inti)
    Text1.Text = strReplaceString
End Sub

Private Sub FindReplace(intCrit As Integer)
    Select Case intCrit
            Case 1
                    strReplaceString = Replace(strReplaceString, "1", "2", , , vbTextCompare)
            Case 2
                    strReplaceString = Replace(strReplaceString, "2", "3", , , vbTextCompare)
    End Select
End Sub

The procedure in the class module, which will replace the content of the text depending on criteria passed to the procedure.
As you hv told in question that u hv 26 different criteria, u can list them in the select case and implement the replace criteria in each case.
Valliappan ANSenior Tech ConsultantCommented:
Good one from atp_expert.

If the Replace need to be individually 26 items, it appears good, but at a stretch 26 items, using a array can solve with minimum coding, IMO.

create a Class

Public From As String
Public To As String

Public Function doReplace(String str) As String
    doReplace = Replace(str, From, To)
End Function

create another class

Private m_Col As New Collection

Public Function Add(String From, String To) As ReplaceRule
    Dim tmp As ReplaceRule
    tmp = New ReplaceRule

    tmp.From = From
    tmp.To = To

    m_Col.add tmp

    Add = tmp
End Function

Public Function NewEnum() As IUnknown
   Set NewEnum = m_Col.[_NewEnum]
End Function

(This can be most easily done using the Class Builder. Tools > Add-ins...)

Now this is where it makes the rest of your code more easy to understand and work with.

' firstly you need to have a ReplaceRules

Dim myReplaceRules As New ReplaceRules

' Then you need to add your Rules to it.

Public Sub Command1_Click()

    myReplaceRules.Add(Text1.Text, Text2.Text)

End Sub

' Now you need to use it.

Public Sub Command2_Click()

    Dim rule As ReplaceRule

    For Each rule In myReplaceRules
        Text3.Text = rule.doReplace( Text3.Text )

End Sub

' Try it out. Your code becomes 4 lines to do it and 1 line to add to it

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.