|
-
Feb 6th, 2001, 07:08 AM
#1
Can someone tell me how to use the Replace() function in an ASP page and have it ignore case?
I.E. I'd want:
Code:
sTemp=Replace(sTemp, "matthew", "Matt")
to return "Matt" wether the original value of sTemp was "Matthew, "MATTHEW" or "matthew".
I though it'd be simple but in ASP the Replace function seems to be case sensitive...after leafing through the help file I found that I could gve it a value of vbTextCompare for the Compare argument, or if I stuck Option Compare Text at the top of the file that it should be case insensitive, but both approaches produce errors (usually "Type Mismatch"). 
For what it's worth the code I've got is:
Code:
Function ElimiSwear(txt)
sTemp = txt
Set oRSElimiSwear = Server.CreateObject("ADODB.RecordSet")
oRSElimiSwear.Open "SELECT word, replacement FROM ElimiSwear", oConn
'Response.Write "#" & vbTextCompare & "#"
If Not oRSElimiSwear.BOF And Not oRSElimiSwear.EOF Then
oRSElimiSwear.MoveFirst
Do Until oRSElimiSwear.EOF
sWord = oRSElimiSwear("word")
sReplacement = oRSElimiSwear("replacement")
If IsNull(sReplacement) Or IsEmpty(sReplacement) Or (sReplacement = "") Then sReplacement = MakeStars(sWord)
sTemp = Replace(sTemp, sWord, sReplacement) ' Damn!! Replace function is case sensitive!!
'iPos = Instr(lcase(sTemp), lcase(sWord))
'Response.Write "#" & iPos & "#"
'Do While iPos > 0
' 'Response.Write "#" & iPos & "#"
' sTemp = Left(sTemp,iPos) & sReplacement & Right(sTemp, Len(sTemp) - iPos - Len(sWord))
' iPos = Instr(lcase(sTemp), lcase(sWord))
'Loop
oRSElimiSwear.MoveNext
Loop
End If
oRSElimiSwear.Close
Set oRSElimiSwear = Nothing
ElimiSwear = sTemp
End Function
Function MakeStars(txt)
For iCounter = 1 To Len(txt)
sTemp = sTemp & "*"
Next
MakeStars = sTemp
End Function
You might see that I had a go at writing my own Replace function but it usually timed out and sometimes it didn't like the Left() function!!!!! 
I also tried: http://www.vb-world.net/tips/tip110.html
But again it moaned about the Left() function.
Am I missing something obvious? Help me!!!!!
-
Feb 6th, 2001, 04:44 PM
#2
I don't know if this helps but I seem to be able to use Replace() with vbTextCompare just fine. For example:
Code:
Dim strTest
Dim strHTML
strTest = "Hello Paul, PAUL, pAuL, paul."
strHTML = strHTML & "<P>Before: " & strTest & "</P>" & vbcrlf
strHTML = strHTML & "<P>After: " & Replace(strTest, "paul", "PWN", 1, -1, 1) & "</P>" & vbcrlf
Response.Write strHTML
...works just as you'd want it to, producing this:
Before: Hello Paul, PAUL, pAuL, paul.
After: Hello PWN, PWN, PWN, PWN.
...for output.
I can get a type mismatch if I omit some of the arguments to Replace(), for example:
Code:
' The following would produce a type mistmatch error.
Replace(strTest, "paul", "PWN", , , 1)
Paul
-
Feb 6th, 2001, 04:53 PM
#3
how 'bout if you set ..then try
Code:
sTemp = Replace(sTemp, "MATTHEW", "Matt")
hope this helps.
-
Feb 6th, 2001, 05:33 PM
#4
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|