Parse a String?

 

I'm using FileWriteArray/FileReadArray to store some data that I need. But I'm stoing the data as a comma-delimited string because I need to be able to store multiple values.


When I get the string back from my file, how can I parse it? I started this project assuming that there would be a "split" type of function found in other languages but I cannot find the equivalent in MQL. I know I could do this with a combination of substrings and what-not but there has to be an easier way.


How can I parse a comme-delimited string in MQL?

 

Try to use this code for your needs.

int start() {
string str="qwerty,123345,ytrewq,54321";
int char[],pos;
string stri[];
for(int i=0;i<StringLen(str);i++)
   {
   if(StringGetChar(str,i)==44)//code ","
      {
      pos++;
      ArrayResize(char,pos);
      char[pos-1]=i;
      //Print (i);
      }
   }
ArrayResize(stri,pos+1);
for(i=0;i<=pos;i++)
   {
   if(i==0)stri[0]=StringSubstr(str,0,char[i]);
   else if(i==pos)stri[i]=StringSubstr(str,char[i-1]+1);
   else stri[i]=StringSubstr(str,char[i-1]+1,char[i]-char[i-1]-1);
   Print("i - ",i," str - ",stri[i]);
   }
   return(0);
}
 

Thanks - this is similar to the code I had envisioned if there was no function


So should I take this to mean that there is no OOTB function that will split it?

 
might find code snippets/ideas/functions here