Solved

Replace Function

Posted on 2003-11-19
11
696 Views
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)
0
Comment
Question by:whose_da_n00b
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 3

Expert Comment

by:monkesdb
Comment Utility
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)

    Next

    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))

    Next

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)
0
 
LVL 3

Expert Comment

by:monkesdb
Comment Utility
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)
Else
    newStr = newStr & Mid(str, i, 1)
End If
0
 

Author Comment

by:whose_da_n00b
Comment Utility
ok, but what if i want to replace whole words, ie "WORD" with "SENTENCE"?
0
 
LVL 9

Expert Comment

by:Valliappan AN
Comment Utility
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

MyArray(1,1)="txttoReplace1"
MyArray(1,2)="Repltext1"
MyArray(2,1)="txttoReplace2"
MyArray(2,2)="Repltext2"
:
:

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

Hope it helps.
0
 

Author Comment

by:whose_da_n00b
Comment Utility
btw it dosent work- "Dim orig As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Dim repl As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz9876543210"" creates an error... HELP...
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:whose_da_n00b
Comment Utility
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*
0
 
LVL 9

Accepted Solution

by:
Valliappan AN earned 130 total points
Comment Utility
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))
    Next
-----------------------------
Hope it helps.
0
 
LVL 9

Expert Comment

by:Valliappan AN
Comment Utility
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.
0
 
LVL 1

Expert Comment

by:atp_expert
Comment Utility
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)
    Next
    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.
0
 
LVL 9

Expert Comment

by:Valliappan AN
Comment Utility
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.

Cheers.
0
 
LVL 3

Expert Comment

by:monkesdb
Comment Utility
create a Class

--------ReplaceRule.cls--------
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

--------ReplaceRules.cls----------------
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 )
    Next

End Sub

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

---------------------------------------------------
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

771 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

11 Experts available now in Live!

Get 1:1 Help Now