[Snippet] StringStrip

Scripts

13 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 SubString(text, 6, StringLength(text)), 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
1
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
1
library StringStrip /* v.1.0.0.0
2
**************************************************
3
*
4
*   Returns a string with particular leading or
5
*   trailing characters removed.
6
* 
7
**************************************************
8
*
9
*   Functions
10
*
11
*       StringStripLeft( string text, string chars ) returns string
12
*
13
*           - Removes leading "chars" from a string.
14
*           - ex: StringStripLeft("Hello", "He") -> "llo"
15
*
16
*       StringStripRight( string text, string chars ) returns string 
17
*
18
*           - Removes trailing "chars" from a string.
19
*           - ex: StringStripRight("Test:::", ":") -> "Test"
20
*
21
*       StringStrip( string text, string chars ) returns string
22
*
23
*           - Removes leading and trailing "chars" from a string.
24
*           - ex: StringStrip(" 500 ", " ") -> "500"
25
*
26
*
27
****************************************************/
28
    
29
    function StringStripLeft takes string text, string chars returns string 
30
        local integer len    = StringLength(text)
31
        local integer ch_len = StringLength(chars)
32
        local integer index  = 0
33
        local integer i = 0
34
        local string  s = ""
35
        /* Preliminary Check */
36
        if ch_len > len then
37
            return text
38
        endif
39
        /* Loop Through Leading Chars */
40
        loop
41
            exitwhen i == len 
42
            set s = SubString(text, i, i+ch_len)
43
            if s == chars then
44
                set index = i+ch_len
45
            elseif index == 0 then
46
                return text
47
            else
48
                return SubString(text, index, len)
49
            endif
50
            set i = i + ch_len
51
        endloop
52
        return ""
53
    endfunction
54
    
55
    function StringStripRight takes string text, string chars returns string 
56
        local integer ch_len = StringLength(chars)
57
        local integer i      = StringLength(text)
58
        local integer index  = 0
59
        local string  s = ""
60
        /* Preliminary Check */
61
        if ch_len > i then
62
            return text
63
        endif
64
        /* Loop Through Leading Chars */
65
        loop
66
            exitwhen i <= 0 
67
            set s = SubString(text, i-ch_len, i)
68
            if s == chars then
69
                set index = i-ch_len
70
            elseif index == 0 then
71
                return text
72
            else
73
                return SubString(text, 0, index)
74
            endif
75
            set i = i - ch_len
76
        endloop
77
        return ""
78
    endfunction
79
    
80
    function StringStrip takes string text, string chars returns string 
81
        set text = StringStripLeft(text, chars)
82
        return StringStripRight(text, chars)
83
    endfunction
84
    
85
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