MQL4 Reference Language Basics Data Types String Type

MQL4 Help as One File:

String Type

The string type is used for storing text strings. A text string is a sequence of characters in the Unicode format with the final zero at the end of it. A string constant can be assigned to a string variable. A string constant is a sequence of Unicode characters enclosed in double quotes: "This is a string constant".

If you need to include a double quote (") into a string, the backslash character (\) must be put before it. Any special character constants can be written in a string, if the backslash character (\) is typed before them.


string svar="This is a character string";
string svar2=StringSubstr(svar,0,4);
Print("Copyright symbol\t\x00A9");
FileWrite(handle,"This string contains a new line symbols \n");
string MT4path="C:\\Program Files\\MetaTrader 4";

To make the source code readable, long constant strings can be split into parts without addition operation. During compilation, these parts will be combined into one long string:

//--- Declare a long constant string
   string HTML_head="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\""
                    " \"\">\n"
                    "<html xmlns=\"\">\n"
                    "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n"
                    "<title>Trade Operations Report</title>\n"
//--- Output the constant string into log

Internal representation of the string type is a structure of 12 bytes long:

#pragma pack(push,1)
struct MqlString
   int      size;       // 32-bit integer, contains size of the buffer, allocated for the string.
   LPWSTR   buffer;     // 32-bit address of the buffer, containing the string.
   int      reserved;   // 32-bit integer, reserved.
#pragma pack(pop,1)

See also

Conversion Functions, String Functions, FileOpen(), FileReadString(), FileWriteString()