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

Replace Function

Posted on 2003-11-19
Last Modified: 2010-05-03
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)
Question by:whose_da_n00b
  • 4
  • 3
  • 3
  • +1

Expert Comment

ID: 9784482
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)

Expert Comment

ID: 9784494
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

Author Comment

ID: 9789143
ok, but what if i want to replace whole words, ie "WORD" with "SENTENCE"?
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Expert Comment

by:Valliappan AN
ID: 9790935
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.

Author Comment

ID: 9790945
btw it dosent work- "Dim orig As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Dim repl As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz9876543210"" creates an error... HELP...

Author Comment

ID: 9792605
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*

Accepted Solution

Valliappan AN earned 130 total points
ID: 9794678
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.

Expert Comment

by:Valliappan AN
ID: 9794695
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.

Expert Comment

ID: 9796216
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.

Expert Comment

by:Valliappan AN
ID: 9802101
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.


Expert Comment

ID: 9804225
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


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

856 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