0

I've been searching, and I found some similar questions, but none of them are solution for me, so what I need is this in Visual Basic:

I have a text (string) and I have two Arrays like this:

Dim data_array_one As String() = {"One", "Two", "Three", "Four"}
Dim data_array_two As String() = {"Five", "Six", "Seven", "Eight"}

What I need is replace every “One” in a text for “Five”, every “Two” for “Six”, and so... I've been using a simple replace function:

text1 = text1.Replace("One", "Five")
text1 = text1.Replace("Two", "Six")
...

But now array contains 24 elements, and every day it increments automatically, so I need something to do it automatically from arrays instead the actual way... Thanks in advanced.

6
  • Is it a 1:1 mapping between arrays one and two ? Commented Feb 4, 2013 at 13:16
  • Yes, I need to replace it, first with first, second with second, third with third, and so... Commented Feb 4, 2013 at 13:18
  • 2
    This is not very optimal but you could try something like for (i=0; i< arrayLength;i++) {text1 = text1.Replace(array1[i], array2[i]);}. Commented Feb 4, 2013 at 13:22
  • Do you need to handle issues like partial matches? Using String.Replace to replace "Four" with "Eight" will also cause "Fourteen" to become a misspelled "Eightteen". Probably not what you want. Commented Feb 4, 2013 at 15:12
  • @ryadavilli It's a VB.NET question so and answer in C# isn't useful. Commented Feb 4, 2013 at 15:37

1 Answer 1

1

You could try this as a simplistic fix.

dim i as single

For i = 0 to data_array_one.getlength(0)
text1 = text1.Replace(data_array_one(i), data_array_two(i))
next

However it isn't very efficient and as tcarvin said in a comment to your question, this could lead to problems with "Four" being replaced in "Fourteen".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.