MQL5 convert string to bool

 

Hello there, this is for information. I have recently struggled to convert string to bool in MQL5.

Thanks to the tip of a friend, I could resolve it. I want to post it here, in case somebody has the same problem.

The source field is a string, which has either "true" or "false".

Important, this must be small letters, not capital letters.

The target field is of type bool.

This conversion will bring back "TRUE" if the source is "true" and "FALSE" if the source is "false".

dataLine.goodForCurrency    = line[4] == "true";   

just to explain: my source-field is in an array, and it is in the 4th place.

the target field is called 

dataLine.goodForCurrency

it is another field in an array

 

You can use e.g. bool x = (bool)StringCompare("false", line[4], false); // 0 (null), if "false"== line[4]

(Just typed not tested!)

Dokumentation zu MQL5: Stringfunktionen / StringCompare
Dokumentation zu MQL5: Stringfunktionen / StringCompare
  • www.mql5.com
Vergleicht zwei Strings gibt das Ergebnis des Vergleichs als Ganzzahl zurück. Parametsr string1 [in]  Der erste String. string2 [in]...