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:
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:
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.
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:
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:
1 library StringStrip /* v.1.0.0.0
2 **************************************************3 *4 * Returns a string with particular leading or5 * trailing characters removed.6 * 7 **************************************************8 *9 * Functions10 *11 * StringStripLeft( string text, string chars ) returns string12 *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 string22 *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 text38 endif39 /* Loop Through Leading Chars */40 loop41 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 text47 else48 return SubString(text, index, len)
49 endif50 set i = i + ch_len
51 endloop52 return ""
53 endfunction54
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 text63 endif64 /* Loop Through Leading Chars */65 loop66 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 text72 else73 return SubString(text, 0, index)
74 endif75 set i = i - ch_len
76 endloop77 return ""
78 endfunction79
80 function StringStrip takes string text, string chars returns string
81 set text = StringStripLeft(text, chars)
82 return StringStripRight(text, chars)
83 endfunction84
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.
