[Snippet] StringStrip

Codes & Snippets

12 years
edited 8 years
One of my favorite string scripts from Python. I can't live without it.

StringStrip will strip the leading or trailing characters from a particular string. This is very, very useful for parsing. Just to show an example, you have a command:
"-gold 500"
Normally, you would read it as [lcode=jass]SubString(text, 6, StringLength(text))[/lcode], and convert it to an integer. But what if someone types it like this?
"  -gold 600   "
That should be perfectly acceptable, but the system will interpret it as "d 600   ". If you try to convert that, it will return 0. There are many ways to approach that problem, but one way is using StringStrip(). Simply:
Code (jass) Select
call StringStrip("  -gold 600  ", " ")
The returned string will be, "-gold 600", ready for reading.

To understand it better, it is kind of like StringReplace() or remove, but with only leading and trailing characters.

Here is the code:
Code (jass) Select
library StringStrip /* v.1.0.0.0
**************************************************
*
*   Returns a string with particular leading or
*   trailing characters removed.
*
**************************************************
*
*   Functions
*
*       StringStripLeft( string text, string chars ) returns string
*
*           - Removes leading "chars" from a string.
*           - ex: StringStripLeft("Hello", "He") -> "llo"
*
*       StringStripRight( string text, string chars ) returns string
*
*           - Removes trailing "chars" from a string.
*           - ex: StringStripRight("Test:::", ":") -> "Test"
*
*       StringStrip( string text, string chars ) returns string
*
*           - Removes leading and trailing "chars" from a string.
*           - ex: StringStrip(" 500 ", " ") -> "500"
*
*
****************************************************/
   
    function StringStripLeft takes string text, string chars returns string
        local integer len    = StringLength(text)
        local integer ch_len = StringLength(chars)
        local integer index  = 0
        local integer i = 0
        local string  s = ""
        /* Preliminary Check */
        if ch_len > len then
            return text
        endif
        /* Loop Through Leading Chars */
        loop
            exitwhen i == len
            set s = SubString(text, i, i+ch_len)
            if s == chars then
                set index = i+ch_len
            elseif index == 0 then
                return text
            else
                return SubString(text, index, len)
            endif
            set i = i + ch_len
        endloop
        return ""
    endfunction
   
    function StringStripRight takes string text, string chars returns string
        local integer ch_len = StringLength(chars)
        local integer i      = StringLength(text)
        local integer index  = 0
        local string  s = ""
        /* Preliminary Check */
        if ch_len > i then
            return text
        endif
        /* Loop Through Leading Chars */
        loop
            exitwhen i <= 0
            set s = SubString(text, i-ch_len, i)
            if s == chars then
                set index = i-ch_len
            elseif index == 0 then
                return text
            else
                return SubString(text, 0, index)
            endif
            set i = i - ch_len
        endloop
        return ""
    endfunction
   
    function StringStrip takes string text, string chars returns string
        set text = StringStripLeft(text, chars)
        return StringStripRight(text, chars)
    endfunction
   
endlibrary


It is mostly useful for spaces, but you can use other characters too--even multiple ones:
"xxxtestxxx" -> StringStripLeft("xxx") -> "testxxx"
"testing" -> StringStripRight("ing") -> "test"
As far as complexity goes, this is a very lightweight function, despite how large it looks. It can be implemented recursively, but recursiveness is generally a bad idea in wc3 speed-wise. It doesn't generate any more strings than it needs, as well.
12 years
That's what I call something lovely :D

Approved!!!!!
10 years
edited 10 years
Hmm, took me a little bit to understand it.
I personally think the names should be swapped. Not sure if I'm right in that.
Unfortunately I did not understand how the third function should be called, if at all.
Edit: nevermind, I got it.
So, basically StringStrip the third function as I understand it should be called StringStripMiddle, the others should be swapped from StringStripRight to StringStripLeft and from StringStripLeft to StringStripRight. Or just leave it all the same and replace the word Middle with Both or the word Sides. Anyway, not sure if a Library name should be the same as a Function name. I guess it compiles or you wouldn't have posted the code, but it could be confusing for the layman.
8 years
so they don't have to set it in Notification Preferences