String find the separator, string substring first part into the enlarged array, string substring the remaining part(s). Repeat. Store the last.
String find the separator, string substring first part into the enlarged array, string substring the remaining part(s). Repeat. Store the last.
int i = 0; int j = 0; //String find i = StringFind(toSplit, separator, 0); //Debug purposes string whatItGives = StringSubstr(toSplit, 0, i); while(StringSubstr(toSplit, i, i) != "") { //String find i = StringFind(toSplit, separator, 0); //String substring string subStr = StringSubstr(toSplit, 0, i); result[j] = subStr; j++; }Of course it doesn't work yet, i need a little more help, i'm getting really confused.
The code is a spaghetti, if you can bring me a little more help then great, if not then not problem.
// Extended StringSplit: string separator int StringSplitX(string toSplit, string separatorX, string& result[]){ int n=0, len=StringLen(separatorX); if( StringLen(toSplit) == 0 || len == 0) return WRONG_VALUE; while(true){ int iSep = StringFind(toSplit, separatorX); ArrayResize(result, n+1); if(iSep < 0){result[n++]= toSplit; return n; } // No sep: Store last one (or only). if(iSep == 0) result[n++]= ""; // Beginning sep: Store empty string. else{ result[n++]= StringSubStr(toSplit, 0, iSep); // Store first one. toSplit = StringSubStr(toSplit, iSep+len); // Remove first through sep. } }Not compiled, not tested, just typed.
Working perfectly, just a few misstyping errors, a missing "}" and a mayus, fixed it and working as expected, i've almost done it too before you answered, so i'll try to finish mine too so i learn from my error, thank you for being kind!
Have a good day William
use the code
StringReplace(toSplit,"#!#",",");
use the code
Thanks for trying to help, but i quote:
"I want the separator to be a string and not a single character, a "solution" would be replace "#!#" into ',' but that's not the idea, i want to use the specific "#!#" string as the separator, i hope i've explained it properly."
Anyway, I'll use yours because besides yours being more efficient, you spent part of your time helping me and i respect that, so the least i can do after i could solve it on my own (with the help you gave me in your first comment) is to use the code you provided
Not to mention that if I hadn't been able to solve the problem my way, maybe your solution would have been my only salvation, so that's another thing I'm grateful for.
Next thing i want to do is understand your code, so i'll go trough it slowly.
Thanks again William!
- Works only if there are no commas in the search string.
- What part of FrannPV: a "solution" would be replace "#!#" into ',' but that's not the idea, i want to use the specific "#!#" string as the separator,was unclear?
Here is some useful and tested string functions.
//+------------------------------------------------------------------+ //| Extended StringSplit: string separator. | //+------------------------------------------------------------------+ /** * Split a string into an array of substrings, using a specified separator string. * Limit [optional]. Specifies the max number of splits to be placed into the array. * Return value is the number of splits in the result array. */ int StringSplit(string string_value,string separator,string &result[],int limit = 0) { int n=0, pos=-1, len=StringLen(separator); while((pos=StringFind(string_value,separator,pos))>=0) { ArrayResize(result,++n); result[n-1]=StringSubstr(string_value,0,pos); if(n==limit) return n; string_value=StringSubstr(string_value,pos+len); pos=-1; } //--- append the last part ArrayResize(result,++n); result[n-1]=string_value; return n; } //+------------------------------------------------------------------+ //| StringContains. | //+------------------------------------------------------------------+ bool StringContains(const string str,const string substr) { if(!StringLen(substr) || StringLen(substr)>StringLen(str)) { return false; } return StringFind(str,substr,0) != -1; } //+------------------------------------------------------------------+ //| StringStartsWith. | //+------------------------------------------------------------------+ bool StringStartsWith(const string str,const string substr) { if(!StringLen(substr) || StringLen(substr)>StringLen(str)) { return false; } return StringSubstr(str,0,StringLen(substr)) == substr; } //+------------------------------------------------------------------+ //| StringEndsWith. | //+------------------------------------------------------------------+ bool StringEndsWith(const string str,const string substr) { if(!StringLen(substr) || StringLen(substr)>StringLen(str)) { return false; } return StringSubstr(str,StringLen(str)-StringLen(substr)) == substr; } //+------------------------------------------------------------------+ //| StringRepeat. | //+------------------------------------------------------------------+ string StringRepeat(const string str,const int count) { string result = ""; for(int i=0; i<count; i++) { result += str; } return result; } //+------------------------------------------------------------------+ //| StringPadRight. | //| Left align strings by right padding to target width ('str '). | //+------------------------------------------------------------------+ string StringPadRight(const string str,const int targetWidth,const string padString = " ") { return str + StringRepeat(padString,targetWidth-StringLen(str)); } //+------------------------------------------------------------------+ //| StringPadLeft. | //| Right align strings by left padding to target width (' str'). | //+------------------------------------------------------------------+ string StringPadLeft(const string str,const int targetWidth,const string padString = " ") { return StringRepeat(padString,targetWidth-StringLen(str)) + str; }
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello everyone, hope you are having a good day. Sorry for my english, i do not speak this language natively.
I'm looking for a way to split a string like it would be done with the classic StringSplit function, but i would like to know how to use a string as a separator, and not only a character.
For example, if i wanted to do it with a character i know it would need to be like this:
The code above would split "toSplit" into 4 pieces and save it to the array, but if i wanted to use the following string: "#!#" as my separator, how should i do this?
I want the separator to be a string and not a single character, a "solution" would be replace "#!#" into ',' but that's not the idea, i want to use the specific "#!#" string as the separator, i hope i've explained it properly.
Thanks!