Results 1 to 4 of 4

Thread: Case insensitive Replace() function in ASP

  1. #1
    matthewralston
    Guest
    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!!!!!

  2. #2
    Guest
    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:&nbsp;&nbsp;" & strTest & "</P>" & vbcrlf
    strHTML = strHTML & "<P>After:&nbsp;&nbsp;" & 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

  3. #3
    Guest
    how 'bout if you set
    Code:
    sTemp = Ucase(txt)
    ..then try
    Code:
    sTemp = Replace(sTemp, "MATTHEW", "Matt")
    hope this helps.

  4. #4
    Guest
    Hey I tried that approach already, billyboy, billyboy!
    I tried that approach already my good friend, billy!
    Didn't work unfortunately...
    But that doesn't bother me...
    I'm so happy, now I have the bug that hurt me

    PWNettle... cheers, mate! It worked!!
    I guess sometimes a simple example is often easier to get right than a big ugly one

    It seems to be that because I missed out some of the arguments in between the code wasn't working... I filled them in like in your code and it worked. I thought that putting in the commas as placeholders was ment to do that but obviously not.

    Anyway, works now...am happy...thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width