[ARCHIVE!] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Can't go anywhere without you - 4. - page 58

 

I've rewritten the string replacement function with arrays:

string StrReplaceTwo(string text){
string textMod[] ={""};
string symbols1[] = {"12","15","19","13","14","16"};

string symbols2[] = {"a","b","c","d","e","f"};


string textNew="";

int textLen = StringLen(text);

for (int i1=0; i1<textLen; i1=i1+2)
{
string TwoSymbols = StringSubstr(text,i1,2);

for (int i2=0; i2<ArraySize(symbols1); i2++)

{
if (symbols1[i2]==TwoSymbols) {textNew = textNew + symbols2[ i2 ] ; i2=ArraySize(symbols1) ; }

}

}

return (textNew);
}


Now it goes through 2 characters per string, e.g. string 1215 will be replaced by ab. But I encountered a problem: the function omits characters that are not in the array and omits the last character if the string has an odd number of characters. Please help change the function so that string 1214153 it would replace with a14b3. Thank you!

 
Lians:

Please help me change the function so that line 1214153 is replaced with a14b3. Thank you!


I don't understand the logic, you have 14 in the array, i.e. it should be replaced with "e". You can mess around with parity, but it's better to add zeros, i.e. replace 3 with 03.
 
Lians:

I've reworked the string character replacement function a bit with arrays:


I think you're getting a bit wacky. If you need a substring replacement function, you can use this option:

// функция ищет matched_text в строке text и заменяет его на replace_text

string StringReplace(string text, string matched_text, string replace_text)
{
   int pos=StringFind(text,matched_text);
   while(pos>=0)
   {
      if(pos==0) text = StringConcatenate(replace_text,StringSubstr(text,StringLen(matched_text)));
      else text = StringConcatenate(StringSubstr(text,0,pos),replace_text,StringSubstr(text,pos+StringLen(matched_text)));
      pos=StringFind(text,matched_text,pos+StringLen(replace_text));
   }
   return(text);
} 

I tweaked the end a bit, I didn't immediately notice the looping feature

 
Roger:

I don't understand the logic, you have 14 in the array, i.e. it should be replaced by "e". You can mess around with parity, but it's better to add zeros, i.e. replace 3 with 03.
Oh, sorry, didn't notice, then let's say 1217153 to a17b3, about parity - adding zeros is fine, can you do that? Or let it omit the last character, I thought it's not important.
 
alsu:

I think you're getting a bit wacky. If you need the substring replacement function, you can use this option:

Thank you! But my strings are generated and the substitution has to be generated too and preferably take 2 characters at a time.
 
How do I set the time frame in which trades are executed? (start time and end time)
 

Lians:
Oh, sorry, didn't notice, then let's say 1217153 to a17b3, about parity - the option of adding zeros would work, can you do that?
string StrReplaceTwo(string text){
 string textMod[] ={""};
 string symbols1[] = {"12","15","19","13","14","16"};

 string symbols2[] = {"a","b","c","d","e","f"};


 string textNew="";

 int textLen = StringLen(text);

 for (int i1=0; i1<textLen; i1=i1+2)
 {
 string TwoSymbols = StringSubstr(text,i1,2);
bool x=true;

 for (int i2=0; i2<ArraySize(symbols1); i2++)

 {
 if (symbols1[i2]==TwoSymbols) {textNew = textNew + symbols2[ i2 ] ; x=false; break;}

 }
if(x) textNew +=StrToInteger(TwoSymbols);
 }

 return (textNew);
 }
Somewhere like this.
 

Thanks, but I can't compile it, it's crashing on a string:

if(x) textNew +=StrToInteger(TwoSymbols);

It's like this: '+=' - both operands are to be numeric

how do i fix it?

 
Lians:

Thanks, but I can't compile it, it's crashing on a string:

It swears like this: '+=' - both operands are to be numeric

How do I fix it?

It does not work for strings in MQL4.

if(x) textNew = textNew + StrToInteger(TwoSymbols);
 
Lians:

Thanks, but I can't compile it, it's crashing on a string:

it says: '+=' - both operands are to be numeric

How do I fix it?

if(x) textNew = textNew + StrToInteger(TwoSymbols);
in mql4 you cannot use += operator for string types