What I’m trying to do is replace string between two references with something else… for example:
dim myString as string
myString = “[!]hello[!] there… how are [!]you[!] doing today?”
Anything between two [!] I am trying to pull out and replace with something else, while leaving everything else intact. I’ve been trying to tinker with inStr, but it’s killin’ me here. Anybody know if an easier solution at this? Could RegEx be a potential candidate? There could be anything between the reference points, and it can contain any amount of words, and start anywhere… not just at the beginning of the string, and finish at the end. Some strings don’t have any, some can contain 7 or 8 references to pull and replace. Again, this is kicking my tail… any help would be appreciated.
You could try splitting the string based on the [!], loop over the array of strings and the join back with the new strings… Or use NthField and replace right in the string…
Function ReplaceAllBetween(sourceString As String, tag As String, replacementString As String) As String
Dim result() As String
Dim i As Integer
result = Split(sourceString, tag)
if Left(sourceString, Len(tag)) = tag then
result.Remove(0)
i = 0
else
i = 1
end if
while i <= result.Ubound
result(i) = replacementString
i = i + 2
wend
return Join(result, "")
End Function
As always… you guys delivered, and quickly! Appreciate the replies… and I’ll miss with the split suggestion. That didn’t even occur to me to try. Thanks!
You don’t need to check the first tag or remove it, especially since that would slow it down. If the string starts with the tag, result(0) will be empty anyway, so the code can be simplified a bit.
Function ReplaceAllBetween(sourceString As String, tag As String, replacementString As String) As String
Dim result() As String
Dim i As Integer
result = Split(sourceString, tag)
for i = 1 to result.Ubound step 2
result( i ) = replacementString
next i
return Join(result, "")
End Function
BTW, the difference in the two approaches is that Alwyn’s code will remove the tags whereas the RegEx-based code I provided will preserve them. Eric, I’m not sure which you wanted, so just be aware. Either approach can be easily modified to do the other.