跳至内容

String Type

字符串类型

字符串类型用于存储文本字符串。文本字符串是Unicode格式字符的序列,其末尾为零。可以将字符串常量赋值给字符串变量。字符串常量是由双引号包围的Unicode字符序列:“这是一个字符串常量”。

如果需要在字符串中包含双引号("),则必须在前面加上反斜杠字符(\)。任何特殊的字符常量都可以写入字符串中,只要在它们之前输入反斜杠字符(\)。

示例:

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";

为了使源代码可读,长字符串可以拆分为多个部分,无需进行加法操作。在编译过程中,这些部分将合并成一个长字符串:

//--- Declare a long constant string
   string HTML_head="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\""
                    " \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
                    "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
                    "<head>\n"
                    "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n"
                    "<title>Trade Operations Report</title>\n"
                    "</head>";
//--- Output the constant string into log
   Print(HTML_head);
  }

字符串类型的内部表示是一个长度为12字节的结构体:

#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)

另请参阅

转换函数字符串函数FileOpen()FileReadString()FileWriteString()

最后更新于