how to process string in mql5

To add comments, please log in or register
XIn Li
1806
XIn Li  

Say it has input like this in EA preset

input string  BL="L1,L2,L3,L4";


how do I process this input into a string array like?

BLS=(L1,L2,L3,L4);


Thanks in advance.

Chris70
543
Chris70  
For an answer you need to specify the variable type of the array BLS (= L1,L2... are substrings or numbers?). String array? (double) numbers array?
XIn Li
1806
XIn Li  
Chris70:
For an answer you need to specify the variable type of the array BLS (= L1,L2... are substrings or numbers?). String array? (double) numbers array?

Thanks. It is a string array. string BLS.

Chris70
543
Chris70  

in case they are numbers I'd try something like this (code not tested!):

StringToArray(BL,BLS);

void StringToArray(string& str,double& arr[],string delimiter=",")
  {
   string strcopy=str;
   int elements=StringReplace(strcopy,delimiter,delimiter)+1;
   ArrayResize(arr,elements);
   for (int i=0;i<elements-1;i++)
     {
      int next_delimiter=StringFind(strcopy,delimiter,0);
      arr[i]=StringToDouble(StringSubstr(strcopy,0,next_delimiter));
      strcopy=StringSubstr(strcopy,next_delimiter+1,-1);
     }
   arr[elements-1]=StringToDouble(strcopy);
  }

if they are just substrings you don't need the conversion StringToDouble of course;

note: the StringReplace function here is just a trick for quickly counting the number of appearances of the delimiter (",")

Chris70
543
Chris70  
StringToArray(BL,BLS);

void StringToArray(string& str,string& arr[],string delimiter=",")
  {
   string strcopy=str;
   int elements=StringReplace(strcopy,delimiter,delimiter)+1;
   ArrayResize(arr,elements);
   for (int i=0;i<elements-1;i++)
     {
      int next_delimiter=StringFind(strcopy,delimiter,0);
      arr[i]=StringSubstr(strcopy,0,next_delimiter);
      strcopy=StringSubstr(strcopy,next_delimiter+1,-1);
     }
   arr[elements-1]=strcopy;
  }

okay, then it's a little easier (code also not tested!)

XIn Li
1806
XIn Li  
Chris70:

okay, then it's a little easier (code also not tested!)

Thanks. I tried to call this function StringToArray(BL,BLS) and it said BLS --constant variable cannot be passed as reference . any idea? 

Chris70
543
Chris70  

the reason is (probably) that you passed directly the input variable "BL", which is constant by definition;

try if it works if you delete the first "&" in the function definiton:

void StringToArray(string str,string& arr[],string delimiter=",")
Chris70
543
Chris70  

OR(!) you don't use a function at all and just modify your BLS array directly - then you don't need to pass anything (sorry, I'm running a backtest right now, that's why I don't test it; hope it helps anyway):

string delimiter=",";
string strcopy=BL;
int elements=StringReplace(strcopy,delimiter,delimiter)+1;
ArrayResize(BLS,elements);
for (int i=0;i<elements-1;i++)
  {
   int next_delimiter=StringFind(strcopy,delimiter,0);
   BLS[i]=StringSubstr(strcopy,0,next_delimiter);
   strcopy=StringSubstr(strcopy,next_delimiter+1,-1);
  }
BLS[elements-1]=strcopy;
XIn Li
1806
XIn Li  
Chris70:

the reason is (probably) that you passed directly the input variable "BL", which is constant by definition;

try if it works if you delete the first "&" in the function definiton:

Thanks a lot. That works.

William Roeder
22383
William Roeder  
string BLS[]; StringSplit(BL, ',', BLS);
Chris70
543
Chris70  
Haha.. totally correct! Now I feel ashamed that I didn't think of that easy solution!
12
To add comments, please log in or register