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:
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 [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:
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:
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
endlibraryIt 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.
