<%
Dim myString1
Dim myString2
Dim txt1, txt2, txt3, txt4
dim FilteredStr
txt1 = "Bob"
txt2 = "Carol"
txt3 = "Ted"
txt4 = "Alice"
myString1 = txt1 & "," & txt2 & "," & txt3 & "," & txt4
myString2 = txt1 & "," & txt2
FilteredStr = ReturnFilteredStr(myString1,myString2) 'function returns new filtered string
WriteFilteredStr FilteredStr 'sub to write the new string to page
Function ReturnFilteredStr(str1,str2)
Dim strToFilter
Dim strWhatToFilter
Dim strFiltered
Dim objRegex
Dim iCnt
Dim iCnt2
Dim fStr
strToFilter = split(str1,",") 'myString1 split at comma
strWhatToFilter = split(str2,",") 'myString2 split at comma
strFiltered = "" 'variable to hold strings that make it through the filter
Set objRegex = New Regexp 'Set new regular expression to handle match and replace duties
objRegex.ignorecase = true
objRegex.global = true
for iCnt = 0 to uBound(strToFilter) 'Loop through myString1
fStr = trim(strtoFilter(iCnt)) 'String to Match
for iCnt2 = 0 to uBound(strWhatToFilter) '2nd loop to cycle through myString2
fStr2 = trim(strToFilter(iCnt2)) 'String pattern
objRegex.Pattern = fStr2
fStr = objRegex.Replace(fStr,"") 'If String to Match = String pattern then String to match is replaced with ""
next
if fStr <> "" then
if strFiltered <> "" then
strFiltered = strFiltered & "," & fStr 'if string to Match made it through the filter its added to the new string
else
strFiltered = fStr
end if
end if
next
Set objRegex = Nothing 'object clean up
ReturnFilteredStr = strFiltered
End Function
Sub WriteFilteredStr(str)
Dim strArr
Dim iCnt
if str <> "" then
strArr = split(str,",")
for iCnt = 0 to uBound(strArr)
Response.Write strArr(iCnt) & "<br />"
next
else
Response.Write "Your filtered string is empty."
end if
End Sub
%>
inta=len(MyString2)
for ia=1 to (len(MyString1)-inta)
if mid(MyString1,ia,inta)=MyString2 then
MyString3=left(MyString1,ia-1)&mid(MyString1,ia+inta)
end if
next
Originally posted by AnonJr
Ask over at http://stackoverflow.com ?
While we do occasionally foray into general programming (and some non-programming) topics, we tend to be a little more tightly focused. You may get a faster result by going to a general programming Q&A site like stackoverflow.com
Originally posted by cripto9t
there are probably many ways to do this. I had some time on my hands and wrote this.
It looks complicated but its not and it may not be what you want, if not, someone may find it useful.Code:<%
Dim myString1
Dim myString2
Dim txt1, txt2, txt3, txt4
dim FilteredStr
txt1 = "Bob"
txt2 = "Carol"
txt3 = "Ted"
txt4 = "Alice"
myString1 = txt1 & "," & txt2 & "," & txt3 & "," & txt4
myString2 = txt1 & "," & txt2
FilteredStr = ReturnFilteredStr(myString1,myString2) 'function returns new filtered string
WriteFilteredStr FilteredStr 'sub to write the new string to page
Function ReturnFilteredStr(str1,str2)
Dim strToFilter
Dim strWhatToFilter
Dim strFiltered
Dim objRegex
Dim iCnt
Dim iCnt2
Dim fStr
strToFilter = split(str1,",") 'myString1 split at comma
strWhatToFilter = split(str2,",") 'myString2 split at comma
strFiltered = "" 'variable to hold strings that make it through the filter
Set objRegex = New Regexp 'Set new regular expression to handle match and replace duties
objRegex.ignorecase = true
objRegex.global = true
for iCnt = 0 to uBound(strToFilter) 'Loop through myString1
fStr = trim(strtoFilter(iCnt)) 'String to Match
for iCnt2 = 0 to uBound(strWhatToFilter) '2nd loop to cycle through myString2
fStr2 = trim(strToFilter(iCnt2)) 'String pattern
objRegex.Pattern = fStr2
fStr = objRegex.Replace(fStr,"") 'If String to Match = String pattern then String to match is replaced with ""
next
if fStr <> "" then
if strFiltered <> "" then
strFiltered = strFiltered & "," & fStr 'if string to Match made it through the filter its added to the new string
else
strFiltered = fStr
end if
end if
next
Set objRegex = Nothing 'object clean up
ReturnFilteredStr = strFiltered
End Function
Sub WriteFilteredStr(str)
Dim strArr
Dim iCnt
if str <> "" then
strArr = split(str,",")
for iCnt = 0 to uBound(strArr)
Response.Write strArr(iCnt) & "<br />"
next
else
Response.Write "Your filtered string is empty."
end if
End Sub
%>
Originally posted by Carefree
Here, this is a bit easier:Code:inta=len(MyString2)
for ia=1 to (len(MyString1)-inta)
if mid(MyString1,ia,inta)=MyString2 then
MyString3=left(MyString1,ia-1)&mid(MyString1,ia+inta)
end if
next
Originally posted by EtymonKinda like "Mr. Feel Good"?
Mr. Carefree ... that kind of has a cool ring to it!![]()