• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[Snippet] RemoveStringSpaces

FileIONameSafety was a pretty silly library with very limited application.
I decided to get rid of it and replace with something more modular and general-purpose.

This library is used to remove the leading and trailing spaces in a string, and it can be used to do what FileIONameSafety did just by doing the following:

JASS:
function FixPlayerName takes player p returns nothing
    call SetPlayerName(p, RemoveLeadingSpaces(GetPlayerName(p)))
endfunction

Code

JASS:
/********************************
*
*   RemoveStringCharacters
*   v1.1.0.0
*   By Magtheridon96
*
*   - This library allows you to remove 
*     leading and trailing spaces from a string 
*     and it ensures minimal string leaks in 
*     doing so.
*
*   API:
*   ----
*
*       - function RemoveLeadingCharacters takes string whichString, string character returns string
*           - Removes all leading instances of character in whichString.
*       - function RemoveLeadingSpaces takes string whichString returns string
*           - Removes all leading spaces in whichString.
*       - function RemoveTrailingCharacters takes string whichString, string character returns string
*           - Removes all trailing instances of character in whichString.
*       - function RemoveTrailingSpaces takes string whichString returns string
*           - Removes all trailing spaces in whichString.
*       - function StringTrimCharacters takes string whichString, string character returns string
*           - Removes all trailing and leading instances of character in whichString.
*       - function StringTrim takes string whichString returns string
*           - Removes all trailing and leading spaces in whichString.
*
********************************/
library RemoveStringCharacters

    function RemoveLeadingCharacters takes string str, string character returns string
        local integer position = 0
        local integer length = StringLength(str)
        
        loop
            exitwhen position == length or SubString(str, position, position + 1) != character
            set position = position + 1
        endloop
        
        return SubString(str, position, length)
    endfunction
    
    function RemoveLeadingSpaces takes string str returns string
        return RemoveLeadingCharacters(str, " ")
    endfunction

    function RemoveTrailingCharacters takes string str, string character returns string
        local integer length = StringLength(str)
        local integer position = length - 1

        loop
            exitwhen position == -1 or SubString(str, position, position + 1) != character
            set position = position - 1
        endloop

        return SubString(str, 0, position + 1)
    endfunction
    
    function RemoveTrailingSpaces takes string str returns string
        return RemoveTrailingCharacters(str, " ")
    endfunction

    function StringTrimCharacters takes string whichString, string character returns string
        return RemoveTrailingCharacters(RemoveLeadingCharacters(whichString, character), character)
    endfunction

    function StringTrim takes string whichString returns string
        return StringTrimCharacters(whichString, " ")
    endfunction
    
endlibrary

// Backwards compatibility
library RemoveStringSpaces requires RemoveStringCharacters
endlibrary

Feel free to comment.
 
Last edited:
Level 3
Joined
Sep 12, 2011
Messages
47
I've wrote "C# string class" with Trim method :p
But it's codded on cJASS, because it's very cool stuff i needed method overloading. :)
JASS:
/************************************** 
* 
*   String Class 
*       v1.0 
*   By Max Karelov aka Ty3uK 
* 
*   -Struct such as "string" class in C# 
* 
*   Requirements: 
*   ------------- 
* 
*        Jass New Gen Pack 5d with latest AdicHelper and JassHelper 
*            Here is download link: 
*                [url]http://www.mediafire.com/?aqfwy8wmrcuf67k[/url]
* 
*   API: 
*   ---- 
* 
*        Methods: 
*            Clone() - returns copy of current copy of String. 
*            Equals(String toCompare) - compare current copy of String with argumented copy of String. Returns boolean. 
*            GetHashCode() - returns hash of current copy of String. 
*            IndexOf(string value) - returns first index of argumented value. 
*            IndexOf(string value, int start) - returns first index of argumented value since start position. 
*            Insert(int start, string value) - returns string with inserted value on a start position. 
*            LastIndexOf(string value) - returns last index of argumented value. 
*            LastIndexOf(string value, int start) - returns last index of argumented value since start position. 
*            Remove(int start) - removes all symbols after start position. Returns string. 
*            Remove(int start, int count) - removes defined count of symbols after start position. Returns string. 
*            Replace(string old, string new) - replacing all old values with new values. Returns string. 
*            Substring(int start) - returns substring of current copy of String after start position. 
*            Substring(int start, int length) - returns substring of current copy of String after start position with defined length. 
*            Trim() - removes all spaces chars before and after text of current copy of String. Returns string. 
*            create(string value) - create new copy of String with defined start value; 
*            destroy() - destroy current copy of String. 
* 
*        Operators: 
*            [index] - returns indexed char of current copy of String. 
*            [index]= - sets defined char of current copy of String. 
*            Text - returns text of current copy of String. 
*            Text = - sets new value of current copy of String. 
* 
*   Cheers: 
*   ---- 
*        SirNikolas 
* 
**************************************/ 
library_once StringClass { 
     #include "cj_types_priv.j"; 
      
     struct String { 
         private string VALUE; 
         readonly int Length; 

         #define { 
             IndexOf(value) = indexOf(value, 0); 
             IndexOf(value, start) = indexOf(value, start); 
              
             LastIndexOf(value) = lastIndexOf(value); 
             LastIndexOf(value, start) = lastIndexOf_Overload(value, start); 
              
             Remove(start) = remove(start); 
             Remove(start, count) = remove_Overload(start, count); 
              
             Substring(start) = substring(start); 
             Substring(start, Length) = substring_Overload(start, Length); 
         } 

         public string operator [] (int index) { 
            return SubString(VALUE, index, index + 1); 
         } 
          
         public void operator []= (int index, string char) { 
             if (StringLength(char) == 1) { 
                 VALUE = SubString(VALUE, 0, index) + char + SubString(VALUE, index + 1, Length); 
             } 
         } 

         public thistype Clone() { 
             return thistype.create(VALUE); 
         } 
          
         public bool Equals (thistype toCompare) { 
             return (Text == toCompare.Text); 
         } 
          
         public int GetHashCode() { 
             return StringHash(VALUE); 
         } 
          
         public int indexOf(string value, int start) { 
             int length = StringLength(value); 
             whilenot (start >= Length) { 
                 if (SubString(VALUE, start, start + length) == value) { return start + length; } 
                 start++; 
             } 
             return -1; 
         } 
          
         public string Insert(int start, string value) { 
             return SubString(VALUE, 0, start) + value + SubString(VALUE, start, Length); 
         } 
          
         public int lastIndexOf(string value) { 
             int length = StringLength(value); 
             for (int i = Length; i > 0; i--) { 
                 if (SubString(VALUE, i - length, i) == value) { return i; } 
             } 
             return -1; 
         } 
          
         public int lastIndexOf_Overload(string value, int start) { 
             int Length = StringLength(value); 
             for (int i = start; i > 0; i--) { 
                 if (SubString(VALUE, i - Length, i) == value) { return i; } 
             } 
             return -1; 
         } 
          
         public string remove(int start) { 
             return SubString(VALUE, 0, start); 
         } 
          
         public string remove_Overload(int start, int count) { 
             return SubString(VALUE, 0, start) + SubString(VALUE, start + count, Length); 
         } 
          
         public string Replace(string old, string new) { 
             int oldLength = StringLength(old); 
             int newLength = StringLength(new); 
             int Length = Length; 
             string out = VALUE; 
             for (int i = 0; i < Length; i++) { 
                 if (SubString(out, i, i + oldLength) == old) { 
                     out = SubString(out, 0, i) + new + SubString(out, i + oldLength, Length); 
                     if (oldLength != newLength) { Length = StringLength(out); } 
                 } elseif ((Length - i) < oldLength) { 
                     break; 
                 } 
             } 
             return out; 
         } 
          
         public string operator Text() { 
             return VALUE; 
         } 
          
         public void operator Text=(string value) { 
             VALUE = value; 
             Length = StringLength(value); 
         } 
          
         public string substring(int start) { 
             return SubString(VALUE, start, Length); 
         } 
          
         public string substring_Overload(int start, int Length) { 
             return SubString(VALUE, start, start + Length); 
         } 
          
          
         public string Trim() { 
             string out = VALUE; 
             int start = 0, end = Length; 
             string temp; 
             loop { 
                 temp = SubString(out, start, start + 1); 
                 exitwhen ((temp != " ") && (temp != "\r") && (temp != "\n")); 
                 start++; 
             } 

             loop { 
                 temp = SubString(out, end - 1, end); 
                 exitwhen ((temp != " ") && (temp != "\r") && (temp != "\n")); 
                 end--; 
             } 
             if (start > end) { return ""; } 
             return SubString(VALUE, start, end); 
         } 

         public static thistype create(string value) { 
             thistype out = thistype.allocate(); 
             out.VALUE = value; 
             out.Length = StringLength(value); 
             return out; 
         } 

     }     
}

Btw, sorry my eng.
 
Last edited:
Updated because yeah.

JASS:
function StringTrim takes string whichString returns string
function StringLTrim takes string whichString returns string
function StringRTrim takes string whichString returns string

or

JASS:
function StringTrim takes string whichString returns string
function StringTrimStart takes string whichString returns string
function StringTrimEnd takes string whichString returns string
 
Top