Re: Replace Array with Array
Use a For loop to loop through the array. This way, you are replacing each and every string separately, but the for loop spares you from having to type it out 52 times.
Re: Replace Array with Array
Quote:
Originally Posted by
NickThissen
Use a For loop to loop through the array. This way, you are replacing each and every string separately, but the for loop spares you from having to type it out 52 times.
I get what you're saying, but there's one problem:
I have no idea where to start.
All I can tell to do is write
I understand that I need to find a specified string in the array and replace it with the corresponding string in the other array, but I do not know how to label each individual string.
Thank you,
-Arightwizard
Re: Replace Array with Array
Maybe like this:
vb Code:
Private Function ReplaceSpl(ByVal originalText As String, ByVal oldArr() As String, ByVal newArr() As String)
For i As Integer = 0 To oldArr.Length
originalText = originalText.Replace(oldArr(i), newArr(i))
Next
Return originalText
End Function
And you can call it like this:
vb Code:
origtext = ReplaceSpl(origtext, old, newtxt)
Re: Replace Array with Array
Quote:
Originally Posted by
Pradeep1210
Maybe like this:
vb Code:
Private Function ReplaceSpl(ByVal originalText As String, ByVal oldArr() As String, ByVal newArr() As String)
For i As Integer = 0 To oldArr.Length
originalText = originalText.Replace(oldArr(i), newArr(i))
Next
Return originalText
End Function
And you can call it like this:
vb Code:
origtext = ReplaceSpl(origtext, old, newtxt)
Thank you for your response, however now I am getting an error, "Index was outside the bounds of the Array."
This is confusing me.
Thanks,
-Arightwizard
Re: Replace Array with Array
The loop runs once too much. The Length property of an array returns the number of items, but since that array is zero based (the first item has index 0), there is no item with index "Length".
For example, suppose your array has 3 items. Their indeces would be 0, 1 and 2. But index 3 would not exist.
So, you just need to loop one time less. I'm sure you can figure that out without a code sample ;)
Re: Replace Array with Array
Quote:
Originally Posted by
NickThissen
The loop runs once too much. The Length property of an array returns the number of items, but since that array is zero based (the first item has index 0), there is no item with index "Length".
For example, suppose your array has 3 items. Their indeces would be 0, 1 and 2. But index 3 would not exist.
So, you just need to loop one time less. I'm sure you can figure that out without a code sample ;)
Aah.. my bad. I didn't test it before posting.
It should have been Length - 1
vb.net Code:
Private Function ReplaceSpl(ByVal originalText As String, ByVal oldArr() As String, ByVal newArr() As String)
For i As Integer = 0 To oldArr.Length - 1 '<-- should be Length -1 here
originalText = originalText.Replace(oldArr(i), newArr(i))
Next
Return originalText
End Function