[RESOLVED] Remove All Occurrences Of A String Between Two Different Strings...
"If the brown fox jumps over another brown fox, while a white fox ducks under another white fox, jump for joy."
I would like to remove every word between the word "fox". For example, I need to remove " jumps over another brown " and " ducks under another white ".
Thank you.
Re: Remove All Occurrences Of A String Between Two Different Strings...
Clarification:
The words will not always be the same, so I can't just Replace(myString, " jumps over another brown") or anything like that. There also might be 14 occurrences between two words, so I can't just say "do it twice". I need to remove everything between any two given words (or characters) every time it occurs in a string.
Re: Remove All Occurrences Of A String Between Two Different Strings...
Ok. So, what is your question? Have you written any code so far?
Use Instr to find where "fox" exists in your original String, and then, with the Instr results, use Mid to take only the pieces of the original String you want and place them into a new String variable.
You might need to put pen to paper to figure out the math involved in the Mid parameters.
Re: Remove All Occurrences Of A String Between Two Different Strings...
Will the Mid find every occurrence or just the first one it comes across?
Re: Remove All Occurrences Of A String Between Two Different Strings...
I used this function to get the string between two strings:
Code:
Public Function GetBetween(ByRef sSearch As String, ByRef sStart As String, ByRef sStop As String, _
Optional ByRef lSearch As Long = 1) As String
lSearch = InStr(lSearch, sSearch, sStart)
If lSearch > 0 Then
lSearch = lSearch + Len(sStart)
Dim lTemp As Long
lTemp = InStr(lSearch, sSearch, sStop)
If lTemp > lSearch Then
GetBetween = Mid$(sSearch, lSearch, lTemp - lSearch)
End If
End If
End Function
...but it only gets the first occurrence, not the second.
Re: Remove All Occurrences Of A String Between Two Different Strings...
If you have the MSDN library installed with your VB 6.0 installation, I would suggest reviewing the documentation for Instr and Mid. That will answer your above question and others you might have about the specifics of those two functions. If you don't have the MSDN library installed, you should be able to track down documentation on those two commands using a Google search for something like:
VB 6.0 Instr MSDN
VB 6.0 Mid MSDN
The quick answer to your above question is, Mid will find 0 occurrences of any string, since that's not what Mid does. It is Instr that does that, and it will only find 1 occurrence at a time. That being said, it is quite possible to find multiple instances of a string inside another string using the Instr command inside of a loop.
Re: Remove All Occurrences Of A String Between Two Different Strings...
Thanks, but you're not understanding the complexity of the problem. The string between two strings will never be the same, however "fox" will always be "fox". If I were to use the Instr command inside of a loop, I would be looking for the same string between two strings, which is not what will be happening. Also, the MSDN Library does not specify an answer to my problem. Thanks for trying. Anyone else?
Re: Remove All Occurrences Of A String Between Two Different Strings...
Try the Split() method and split on 'fox'
Re: Remove All Occurrences Of A String Between Two Different Strings...
This RegEx-based solution seems to produce the expected results:
Code:
Public Function RemoveStringsBetween(ByRef DelimStr As String, _
ByRef SrcStr As String, _
Optional ByVal IgnoreCase As Boolean, _
Optional ByVal MultiLine As Boolean) As String
With CreateObject("VBScript.RegExp")
.Global = True
.IgnoreCase = IgnoreCase
.MultiLine = MultiLine
.Pattern = "(" & DelimStr & ")" _
& IIf(MultiLine, "[\s\S]+?", ".+?") _
& "(" & DelimStr & ")"
RemoveStringsBetween = .Replace(SrcStr, "$1$2")
End With
End Function
Code:
? """"; RemoveStringsBetween("fox", "If the brown fox jumps over another brown fox, while a white fox ducks under another white fox, jump for joy."); """"
"If the brown foxfox, while a white foxfox, jump for joy."
Re: Remove All Occurrences Of A String Between Two Different Strings...
Quote:
Originally Posted by
mcoulter876
Thanks, but you're not understanding the complexity of the problem. The string between two strings will never be the same, however "fox" will always be "fox". If I were to use the Instr command inside of a loop, I would be looking for the same string between two strings, which is not what will be happening. Also, the MSDN Library does not specify an answer to my problem. Thanks for trying. Anyone else?
LOL...whatever dude. The MSDN library doesn't just vomit out code needed for every class assignment out there. It tells you how to make use of the commands, and it is up to you to piece those together to accomplish your goal.
You can accomplish exactly what you are looking for with Instr and Mid commands. Well, I can. Maybe you can't.