PDA

View Full Version : upper/lower case replaces


ghell
01-16-2004, 09:52 PM
well, if i want to replace a chunk of text, it only replaces if it is exactly the right case (in this case i am using the commonly used [ b] for <b>

but if i put [ B] in it will not replace it unless i created an entry in the table i am looking up the replaces from in hte correct case, this would be fine if i were just using B, but if i am using words, i would have to do, for example, quote, QUOTE, Quote qUote QUote..... etc, so is there any way of using LCase or UCase or any other method to make it ignore the case, i tried doing a replace with LCase and then the same replace with UCase, but i get the error "invalid use of replace: null"

oracleguy
01-17-2004, 01:38 AM
Well you most likely don't want to use the LCase or UCase methods on the text because it will lose all case in the entire text. A better idea would be to use regular expressions to find and replace the text since you can set it to ignore case.

ghell
01-17-2004, 09:40 AM
err....whats a regular expression?

i wasnt changing the case of the message, just trying to run the replace with lower case then upper case, so i wasnt losing the case of the message anyway, just the case in the []code

"regular expression" rings a bell but i dont know what it is or how i would use it to replace

A1ien51
01-17-2004, 05:15 PM
This is what I do in .NET, not 100% if it is ASP format...
veRegEx = New Regex("\[[bB]\]")
ticketBody = veRegEx.Replace(ticketBody, "<b>")

Eric

glenngv
01-19-2004, 09:53 AM
The Replace() function has parameters that you can specify to ignore the case when comparing.

Replace(String, FindSubstring, ReplaceSubstring, [Start], [Count], [Compare]) (http://www.devguru.com/Technologies/vbscript/quickref/replace.html)

M@rco
01-19-2004, 01:22 PM
With a slightly modified RegEx, you can handle opening and closing tags with a single replace:RE.Pattern="\[(/|)b\]"
RE.Replace("test", "<\1b>")Output:<b>test</b>

If you're doing similar replaces (i.e. [i] to <i>, to <u>, etc.) then you can be even sneakier...RE.Pattern="\[(/|)(b|u|i)\]"
RE.Replace("[u]test", "<\1\2>")Output:<b><u>test</u></b>

;)

EDIT: made a few corrections (code was untested, still isn't). Got my escapes and slash directions confused... it's going to be one of those days...! :p

ghell
01-20-2004, 04:34 PM
muhahaha.. excelent (mr burns impression or something:D)

ill try that normal replace as soon as i get home, is it just
replace(MsgContent,"[ b]","<b>",,,1)

will those RE..... things work in classic asp, and if not can i just convert classic asp to asp.net by changing the extention to .aspx instead of .asp and then inserting any asp.net code into it, or are there things that worked in asp and not in .net that i would need to change or remove.. cos.. that would be a pain in the arse :p lol

ghell
01-20-2004, 05:19 PM
ok with ,,,true i get "Invalid procedure call or argument: 'replace'" and with 1, 0, and other boolean type stuff i get "type mismatch"

what am i doing wrong now :(

EDIT: the RE method shows this:
Cannot use parentheses when calling a Sub
RE.Replace("|b]|u]test|/u]|/b]", "<\1\2>")
-----------------------------------------^

glenngv
01-21-2004, 04:12 AM
There are no boolean arguments for the Replace(). The possible values for the Compare argument is 0, 1 or 2.
You must specify 1 to ignore the case. If you specify the Compare argument, you must also specify the other optional arguments. You didn't specify the Start and Count arguments that's why you encountered type mismatch error.

NewContent = replace(MsgContent,"[ b]","<b>",1,-1,1)

Since you don't know how many times a substring appears in a string, you must specify -1 in the Count argument.

M@rco
01-21-2004, 10:54 AM
Originally posted by glenngv
There are no boolean arguments for the Replace(). The possible values for the Compare argument is 0, 1 or 2.You're correct on the rest, but only 0 (vbBinaryCompare, i.e. case-sensitive) and 1 (vbTextCompare, i.e. case-insensitive) are valid Compare values. I don't know where you get that extra 2 from!?

And just so that everyone is aware, False in VBScript has a numeric value of 0, and True has a value of -1 (not 1, as many of you may think).

;)

M@rco
01-21-2004, 11:42 AM
In case you didn't notice it in glenngv's post, RegEx.Replace() is a function, and returns a value. It doesn't modify the input argument(s).

Since you are calling it without capturing the return value, VBScript treats it as a sub, and hence you are not permitted to use brackets around the arguments.

Therefore you should use the correct function calling syntax "value = function(param1,param2)" to resolve this.

glenngv
01-22-2004, 02:52 AM
Originally posted by M@rco
I don't know where you get that extra 2 from!?


2 = VBDataBaseCompare (http://www.devguru.com/Technologies/vbscript/quickref/replace.html) - Compare information inside database

But I don't know how it works. :D

M@rco
01-22-2004, 10:31 AM
Interesting! That comparison option isn't in my reference help file here at work, but now that I see it, it looks quite familiar, so perhaps it's in the copy that I have at home.

I seem to recall trying to find more information on this comparison option, but finding nothing (and I've had the same result just now). Can anyone provide an example illustrating the difference between all three comparison methods?

PS - I hereby retract the first paragraph of my earlier post!! ;)

ghell
01-22-2004, 04:37 PM
thanks for that, higlighting that i need "1,-1" in there really helped, but.. why is true -1, i mean, i have done what i asked help for now, but -1 just makes no sense :p do you get -1 if you do int(true) or something like that, you probably get a mismatch error or something, im at college atm so i cant try it

oh well, do you ever realy need to use -1 instead of true?

thanks for the help everyone btw, its been very....err....helpful:thumbsup:

M@rco
01-22-2004, 06:08 PM
Originally posted by ghell
why is true -1, i mean, i have done what i asked help for now, but -1 just makes no sense :p do you get -1 if you do int(true) or something like that, you probably get a mismatch error or something, im at college atm so i cant try itYes, CInt(True) equals -1.

Originally posted by ghell
do you ever realy need to use -1 instead of true?True = 1 in most languages, but it's not in VBScript, so you should always use enumerated constants (e.g. True, False, vbBinaryCompare, adOpenStatic) instead of their numeric value for precisely this reason!

Other good reasons (as if that wasn't enough) are that it improves the readability of code, and aids futureproofing (e.g. if the value of adOpenStatic changes in a future version of ADO, your constant will still be correct, where as the hardcoded numeric value in your code will not).