Download MetaTrader 5

stringbuilder or stringbuffer in MQL4

To add comments, please log in or register
jackee1234
197
jackee1234  
Is there something like a stringbuilder (Java) or stringbuffer (C#) in MQL4 that facilitates fast string append?
Carl Schreiber
7289
Carl Schreiber  

StringConcatenate()

StringFormat()


StringAdd()


Just type them into the editor place the cursor above one and press F1 to get the details to use them.
JC
1515
JC  
jackee1234:
Is there something like a stringbuilder (Java) or stringbuffer (C#) in MQL4 that facilitates fast string append?

Not as such, no. There's nothing which defers the creation of a large contiguous memory block into a single final operation, instead of building the string incrementally - no string-builder class, and no alternative such as creating an array of strings and then doing a join operation on it.

However, StringAdd() is vastly faster than StringConcatenate(), and has performance broadly in line with what you'd expect from a string-builder.

JC
1515
JC  
JC:

However, StringAdd() is vastly faster than StringConcatenate(), and has performance broadly in line with what you'd expect from a string-builder.

... A wrapper such as the following around StringAdd() does improve the performance on large strings compared to a simple repeated concatenation:

class StringBuilder {
   private:
      string large;
      string small;

      void Collapse() {
         StringAdd(large, small);
         small = "";      
      }      
   
   public:
      int _STRING_CONCAT_THRESHOLD;
      
      StringBuilder() {
         large = "";
         small = "";
         _STRING_CONCAT_THRESHOLD = 100000;
      }

      void Append(string X) {
         if (StringLen(X) > _STRING_CONCAT_THRESHOLD) {
            Collapse();
            StringAdd(large, X);
         } else {
            StringAdd(small, X);
            if (StringLen(small) > _STRING_CONCAT_THRESHOLD) {
               Collapse();
            }            
         }
      }
      
      string ToString() {
         Collapse();
         return large;
      }   
};
To add comments, please log in or register