Interesting: StringFind() and StringReplace() on char "\n"

 
string s="\n"+"Bye"+"\n"+"Bye"+"\n";
Alert (StringFind(s,"\n",0)," ",StringReplace(s,"\n"," ") );

return: StringFind() --> -1

           StringReplace() --> 3

 
endy5:

[...]

I'd guess the following:

  • When processing the parameters for Alert(), MT4 evaluates the StringReplace() before the StringFind()
  • StringReplace() modifies the s variable (i.e. modifies in place, rather than returning the modified string)
  • So, StringFind() is called after StringReplace() has been called, and therefore after the \n characters have been replaced with spaces 
  • Therefore, StringFind() says that there are no \n characters in the string

 
jjc:

I'd guess the following:

  • When processing the parameters for Alert(), MT4 evaluates the StringReplace() before the StringFind()
  • StringReplace() modifies the s variable (i.e. modifies in place, rather than returning the modified string)
  • So, StringFind() is called after StringReplace() has been called, and therefore after the \n characters have been replaced with spaces 
  • Therefore, StringFind() says that there are no \n characters in the string

hmmm I wrote Alert code to two rows:

Alert (StringFind(s,"\n",0));  // return: 0
Alert(StringReplace(s,"\n"," ") );  // return: 3

strange behavior MT4 (for me)

 

The order that parameters are processed is not defined. Therefor your original alert did the replace first, creating a new s. Then the find proceeded, on the no longer existing string.

See int b=(a++)*3;  // invalid expression

 
endy5:

hmmm I wrote Alert code to two rows:

strange behavior MT4 (for me)

What is strange ? That's normal results.
 

Once again, the MQL manual has a rather strange approach in expressing "notions". The expression int b=(a++)*3 is not invalid, as if invalid means that it will generate a compilation error.

All in all , it will work as described in that very page

The prefix increment (++i) and decrement (--k) are applied to the variable right before this variable is used in an expression.

Post-increment (a++) and post-decrement (k--) are applied to the variable right after this variable is used in an expression"

 

As an example, the following part  is working as expected

 

int a=5; int b=(a++)*5; int c=(++a)*5;
    
Alert(a," ",b," ",c);

 

Regarding OP's question, it looks like there is a "bug". I put double quotes on it because it seems that the functions are regarded as "imported";  as such they are, as  have been said, evaluated form right to left. Still, please pay attention to the manual saying regarding StringFind()

"Return Value

Returns position number in a string, from which the searched substring starts, or -1, if the substring is not found" 

 As such, it is legitimate to return 0

 

 EDIT at the manual, at page Functions > Function call, we can read

" The order of expressions calculation and the order of values loading are not guaranteed "

I guess that statement is above and beyond any rules regarding precedence rules hehe

 

best regarsds 

 

Demos:

All in all , it will work as described in that very page

The prefix increment (++i) and decrement (--k) are applied to the variable right before this variable is used in an expression.

Post-increment (a++) and post-decrement (k--) are applied to the variable right after this variable is used in an expression"


" The order of expressions calculation and the order of values loading are not guaranteed "

I have seen a similar issue in other programming languages, and one possible solution is to "force" the expression(s) to be calculated in the order you want them to be is by using parenthesis (or brackets as some people call them).  Some languages actually do have a default calculation order, like all multiplication and division before all addition and subtraction, and those processed left to right in order that they appear.  But when there is no clear hierarchy, this can lead to programming headaches trying to track down a problem.  The increment and decrement examples listed above look like they function the exact same as I have seen done in Java, and I presume C as well does it this way.
 

Hi,

Is there a way to have wildcard in symbom. For example, all USD pairs (EURUSD, AUDUSD, etc) represented with wildcard? I mean replacing first 3 characters with wildcard to mean, any pair that ends with USD.


thanks

 
s00071609 #: any pair that ends with USD.
if( StringFind( sym, "USD") == 0) //Begins with USD
if( StringFind( sym, "USD") > 0)   //Ends with USD
Reason: