Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 738
  • Last Modified:

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)
0
whose_da_n00b
Asked:
whose_da_n00b
  • 4
  • 3
  • 3
  • +1
1 Solution
 
monkesdbCommented:
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
 
monkesdbCommented:
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
 
whose_da_n00bAuthor Commented:
ok, but what if i want to replace whole words, ie "WORD" with "SENTENCE"?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Valliappan ANCommented:
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
 
whose_da_n00bAuthor Commented:
btw it dosent work- "Dim orig As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Dim repl As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz9876543210"" creates an error... HELP...
0
 
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*
0
 
Valliappan ANCommented:
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
 
Valliappan ANCommented:
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
 
atp_expertCommented:
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
 
Valliappan ANCommented:
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
 
monkesdbCommented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now