?
Solved

Replace Function

Posted on 2003-11-19
11
Medium Priority
?
735 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
[X]
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
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 3

Expert Comment

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

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

Author Comment

by:whose_da_n00b
ID: 9789143
ok, but what if i want to replace whole words, ie "WORD" with "SENTENCE"?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 9

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

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
ID: 9790945
btw it dosent work- "Dim orig As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Dim repl As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz9876543210"" creates an error... HELP...
0
 

Author Comment

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

Accepted Solution

by:
Valliappan AN earned 390 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))
    Next
-----------------------------
Hope it helps.
0
 
LVL 9

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.
0
 
LVL 1

Expert Comment

by:atp_expert
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)
    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
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.

Cheers.
0
 
LVL 3

Expert Comment

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

719 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