Warcraft 3 documentation
vJASS & Zinc Documentation
For the latest documentation about how it works vJASS and Zinc language layers for Warcraft III, please follow these links:
Jasshelper documentation - Zinc documentation - WC3 Optimizer documentation

[Snippet] StringStrip No New Posts Codes & Snippets

Started by
Purgeandfire

0 Members and 1 Guest are viewing this topic.

Rating

Average Score - 5 / 5

« Created: June 29, 2018, 08:52:00 AM by moyack »

[Snippet] StringStrip
on: August 15, 2013, 08:36:50 PM
Category: String handling
Language: vJASS

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
  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
  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.
« Last Edit: December 19, 2017, 08:02:13 PM by moyack »



[Snippet] StringStrip
Reply #1 on: September 13, 2013, 06:52:36 AM

That's what I call something lovely :D

Approved!!!!!


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 »



Re: [Snippet] StringStrip
Reply #3 on: June 28, 2018, 03:46:55 AM

so they don't have to set it in Notification Preferences



 

Started by PitzerMike

Replies: 0
Views: 1637
Codes & Snippets

Started by Purgeandfire

Replies: 0
Views: 1713
Codes & Snippets

Started by Bribe

Replies: 0
Views: 1968
Codes & Snippets

Started by moyack

Replies: 0
Views: 9880
Codes & Snippets

Started by Magtheridon96

Replies: 1
Views: 8662
Codes & Snippets
Vivir aprendiendo.co - A place for learning stuff, in Spanish   Chaos Realm - The world of Game modders and wc3 addicts   Diplo, a gaming community   Power of Corruption, an altered melee featuring Naga and Demon. Play it now!!!   WC3JASS.com - The JASS Vault + vJASS and Zinc   Jetcraft - A Starcraft II mod   WormTastic Clan (wTc)   Warcraft RESOURCES Reforged: Modelos, mapas, proyectos y mas...