how to process string in mql5

 

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.

 
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?
 
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.

 

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 (",")

 
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!)

 
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? 

 

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=",")
 

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;
 
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.

 
string BLS[]; StringSplit(BL, ',', BLS);
 
Haha.. totally correct! Now I feel ashamed that I didn't think of that easy solution!
Reason: