[Last Call] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

Replace help

Hi how can I replace multiple strings instead of this:::
RT.Text = Replace(RT.Text, "dim", "Dim")
RT.Text = Replace(RT.Text, "as", "As")
RT.Text = Replace(RT.Text, "integer", "Integer")
RT.Text = Replace(RT.Text, "string", "String")
RT.Text = Replace(RT.Text, "variant", "Variant")
RT.Text = Replace(RT.Text, "long", "Long")
RT.Text = Replace(RT.Text, "boolean", "Boolean")
RT.Text = Replace(RT.Text, "byte", "Byte")
RT.Text = Replace(RT.Text, "currency", "Currency")
RT.Text = Replace(RT.Text, "private sub", "Private Sub")

Is there an easier way to replace these strings instead of having to keep writing them line by line like above code
  • 2
  • 2
  • 2
  • +3
2 Solutions
this looks like a job for a text editor
Do you have source code in a textbox, and are trying to do some cleanup on the code?

Carl TawnSystems and Integration DeveloperCommented:
Hmm, you could put all the combinations into an array and loop through them doing the replace.

Something like:

Dim arrVals(2, 10) As String

Private Sub Form_Load()

    arrVals(0, 0) = "dim": arrVals(1, 0) = "Dim"
    arrVals(0, 1) = "as": arrVals(1, 1) = "As"
    arrVals(0, 2) = "integer": arrVals(1, 2) = "Integer"
    arrVals(0, 3) = "string": arrVals(1, 3) = "String"
    arrVals(0, 4) = "variant": arrVals(1, 4) = "Variant"
    arrVals(0, 5) = "long": arrVals(1, 5) = "Long"
    arrVals(0, 6) = "boolean": arrVals(1, 6) = "Boolean"
    arrVals(0, 7) = "byte": arrVals(1, 7) = "Byte"
    arrVals(0, 8) = "currency": arrVals(1, 8) = "Currency"
    arrVals(0, 9) = "private sub": arrVals(1, 9) = "Private Sub"

End Sub

Private Sub cmdReplace_Click()

    Dim i As Integer

    For i = 0 To UBound(arrVals, 2) - 1
        RT.Text = Replace(RT.Text, arrVals(0, i), arrVals(1, i))
    Next i

End Sub

Then all you have to do is increase the size of the array to add any other keywords you want to replace on.

Hope this helps.
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.

nffvrxqgrcfqvvcAuthor Commented:
Sorry but the array is just as much coding as using the replace..as you notice that all I am doing is change lowercase to capitital case...

dim = Dim

I am using a richtextbox...Im just creating a vidual basic style code viewer...so the special characters are the keyword color which is blue

So if someone type in   dim test as string it would replace it like the vb source code format

Dim = blue color test=black As=blue color String=blue color
Dim test As String

I am just trying to strip all the replace code down so i dont have to keep adding it like the code I posted..I had an idea to just capitilaze each word after the space..so im experimenting
nffvrxqgrcfqvvcAuthor Commented:
'nevermind I got it to work

 Dim nPos As Integer
    nPos = RT.SelStart
    RT.Text = StrConv(RT.Text, vbProperCase)
    RT.SelStart = nPos
Hey, that is a great new tip!  vbProperCase....glad you resolved it!
Is the text / string in a text file to start with or do you have this string within a text box ? If it is in a text file then obviously you would need something like the FSO to read the text file into a variable or textbox or something like that before you could go through it and search for the words you wanted to replace.

You could have 2 forms, one with the textbox control that had all the text in it and then the 2nd form would have 2 textbox's one with a label next it to search for any words in that string and the 2nd textbox to replace any of the words it finds with the text you enter in the 2nd textbox ie :

Find      : { dim } <--- TextBox
Replace : { Dim } <-- TextBox
Replace        <---- command button

Then in the command button you would have the code that looped through the string and if it found any words that matched the first textbox ie dim then it would replace them with Dim and then you just type each of the words you wanted to find and replace into the textbox's.

I am not sure if this is what you wanted, I mean if you could ellaborate on what exactly you want it to do or how you want it to work, then obviously that would help us with regards to giving you a solution that you are after or are happy with.

If you could please post back with a more detailed description :)
Here you go:

Private Function ReplaceMulti(MainString As String, ParamArray Info() As Variant) As String

    Dim i As Integer
    ReplaceMulti = MainString
    For i = 0 To UBound(Info) Step 2
        ReplaceMulti = Replace(ReplaceMulti, Info(i), Info(i + 1))
End Function

Call it like:

MsgBox ReplaceMulti(Text1.Text, "a", "A", "b", "B", "c", "C")

which will replace a with A, b wth B, c with C, etc., you can put in an infinate amount.
Have you found a solution yet?

Featured Post

Technology Partners: 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!

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