Hi sebastian,

s = string and t = tag - that should help with using the function...

Well, I can briefly explain it.

It begins by checking whether you passed in for a string is actually a
string. I had this function hooked into a data driven series of processes,
so the actual string I was working with was on occasion null...

We set the tag var t to upper case, and set the string we are stripping to
upper case since lastIndexOf is a case sensitive method. Note that we don't
actually change the string - we assign the upper case version of the string
to upString. oT and cT are open tag and close tag. We don't need > for those
strings since we want to hit /> as well as > for self closing tags. i is our
index, we set it to the end of the string since we will be going in reverse.
I think I had originally done this to avoid throwing off the current index
when going through the string (this would happen since the string length
would change) but no longer seems relevant. In any case, it should be no
different than going forward since we copy the string to begin with.

I can see how it might have gotten confusing when I used a, b, c, and d as
index vars. I used to code like this, and I sometimes still do when I don't
think anyone will need to read the code but less so than I used to. It's
also sometimes easier to think with arbitrary vars instead of trying to
categorize them.

So, we start by defining c and d, which are the open and close tags we are
looking for. var a serves as the tag closer to the end of the string. If a
is -1 that just means that the opening or closing tag was never there to
begin with, or has been completely removed. Now that I look at this, I don't
think we need else there :) break would skip the rest of loop...

So, we run indexOf on ">" starting from a to get the end of the tag and
assign the result to b. We then set the string to the substring that is the
beginning of the string to a, to the substring starting from b to the end of
the string. This removes the tag...

But not the content between the tag... Good thing you made me go through
this again sebastian :S

Eh...

function stripTagAndContent(s:String, t:String):String {
    if(typeof(s) == 'string'){
        t = t.toUpperCase();
        var i:Number = s.length-1, oT:String = '<'+t, cT:String = '</'+t,
upString:String = s.toUpperCase();
        while(true){

            var otIndex:Number = upString.lastIndexOf(oT, i);
            var ctIndex:Number = upString.lastIndexOf(cT, i);

            if(otIndex+ctIndex == -2) break;
            if(otIndex>ctIndex) ctIndex = otIndex; //this is self closing or
dangling

            var endIndex:Number = s.indexOf('>', ctIndex)+1;
            s = s.substring(0, otIndex) + s.substring(endIndex, s.length);
            i = otIndex-1;

        }
    } else s = '';
    return s;
}

This one should be easier to read too :)

H

On Wed, Aug 27, 2008 at 2:16 PM, sebastian <[EMAIL PROTECTED]> wrote:

> Looks cool, but any chance you could replace your micro-variable names with
> real-world words for easy reading? Reminds me of moments when I decide I
> need to run for coffee at work...
> ;)
>
>
> H wrote:
>
>> Hey Mike,
>>
>>        public static function stripTag(s:String, t:String):String {
>>            if(typeof(s) == 'string'){
>>                t = t.toUpperCase();
>>                var i:Number = s.length-1, oT:String = '<'+t, cT:String =
>> '</'+t, upString:String = s.toUpperCase();
>>                while(true){
>>                    var c:Number = upString.lastIndexOf(oT, i);
>>                    var d:Number = upString.lastIndexOf(cT, i);
>>                    var a:Number = Math.max(c, d);
>>                    if(a==-1) break;
>>                    else {
>>                        var b:Number = s.indexOf('>', a)+1;
>>                        s = s.substring(0, a) + s.substring(b, s.length);
>>                        i = a-1;
>>                    }
>>                }
>>            } else s = '';
>>            return s;
>>        }
>>
>> A little something I had laying around...
>>
>> H
>>
>> On Wed, Aug 27, 2008 at 1:00 PM, Mendelsohn, Michael <
>> [EMAIL PROTECTED]> wrote:
>>
>>  Perfect answer, H.
>>>
>>> (Too bad it's AS2.)  :-(
>>>
>>> Thanks!
>>> - MM
>>>
>>>
>>> _______________________________________________
>>> Flashcoders mailing list
>>> Flashcoders@chattyfig.figleaf.com
>>> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>>>
>>>  _______________________________________________
>> Flashcoders mailing list
>> Flashcoders@chattyfig.figleaf.com
>> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>>
>>  _______________________________________________
> Flashcoders mailing list
> Flashcoders@chattyfig.figleaf.com
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to