how to process string in mql5
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!)
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;
string BLS[]; StringSplit(BL, ',', BLS);
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.