Author Topic: [Snippet] StringStrip  (Read 9858 times)

  • Rookie - level 1
  • *
  • Posts: 41
  • WC3 Models: 0
  • WC3 Tutorials: 0
  • WC3 Tools: 0
  • WC3 Maps: 0
  • WC3 Skins: 0
  • WC3 Icons: 0
  • Reputation: 13
    • View Profile
[Snippet] StringStrip
« on: August 15, 2013, 08:36:50 PM »
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.

  • Site Owner
  • Administrator
  • Starter - level 4
  • *
  • Posts: 979
  • WC3 Models: 5
  • WC3 Tutorials: 11
  • WC3 Tools: 10
  • WC3 Maps: 11
  • WC3 Skins: 6
  • WC3 Icons: 2
  • Reputation: 1153
  • Site Admin - I love fix things
    • View Profile
    • Blizzard Modding Information Center
[Snippet] StringStrip
« Reply #1 on: September 13, 2013, 06:52:36 AM »
That's what I call something lovely :D

Approved!!!!!


We can give you full hosting for your projects. Not only a forum, a complete page!!.

A custom altered melee map where you can play Naga and Demons. Featuring a nice additions in features and game style. Check it out!!
Save your important files fast and easy

  • Newbie - level 2
  • **
  • Posts: 21
  • WC3 Models: 0
  • WC3 Tutorials: 0
  • WC3 Tools: 0
  • WC3 Maps: 0
  • WC3 Skins: 0
  • WC3 Icons: 0
  • Reputation: 1
    • View Profile
Re: [Snippet] StringStrip
« Reply #2 on: August 31, 2015, 10:37:38 AM »
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.
« Last Edit: August 31, 2015, 10:51:52 AM by sankaku »

  • Lurker - level 1
  • *
  • Posts: 1
  • WC3 Models: 0
  • WC3 Tutorials: 0
  • WC3 Tools: 0
  • WC3 Maps: 0
  • WC3 Skins: 0
  • WC3 Icons: 0
  • Reputation: 0
  • User
    • View Profile
Re: [Snippet] StringStrip
« Reply #3 on: January 07, 2016, 07:13:58 AM »
Can recur or not. Like.

 

[Snippet] Group Units in line, with distance and optional spread-value

Started by rvonsonsnadtzBoard Codes & Snippets

Replies: 5
Views: 10980
Last post March 13, 2013, 06:52:16 PM
by rvonsonsnadtz
[Snippet] Single Player Detector

Started by moyackBoard Codes & Snippets

Replies: 12
Views: 20307
Last post March 13, 2013, 12:48:49 PM
by Magtheridon96
[Snippet] Simple Unit Indexer

Started by moyackBoard Codes & Snippets

Replies: 17
Views: 24910
Last post September 30, 2013, 08:07:19 AM
by moyack
[Snippet] Disable Transmission Skip

Started by PurgeandfireBoard Codes & Snippets

Replies: 2
Views: 8354
Last post July 02, 2013, 01:34:55 PM
by Purgeandfire
[Snippet] Timed Effects

Started by moyackBoard Codes & Snippets

Replies: 0
Views: 6977
Last post January 09, 2012, 07:03:32 PM
by moyack
Blizzard Modding Information Center Starcraft II Modding Information Center Wacraft III Modding Information Center WC3JASS.com - The JASS Vault Chronicles of Darkness - A Warcraft III mod Jetcraft - A Starcraft II mod Troll Smash - A Warcraft III Arena
  Mod DB - Change the Game Power of Corruption - A Warcraft III altered melee map Chaos Realm - The world of Game modders and wc3 addicts Follow us on Facebook!!