跳至内容

Character Constants

字符常量

在MQL4中,字符串中的元素是Unicode字符集中的索引。它们是十六进制值,可以转换为整数,并且可以通过加法和减法等整数运算来操作。

任何用引号括起来的单个字符或作为’\x10’表示的十六进制ASCII代码都是字符常量,其类型为ushort。例如,‘0’类型的记录是一个数值30,对应于字符表中的零索引。

示例:

void OnStart()
  {
//--- define character constants
   int symbol_0='0';
   int symbol_9=symbol_0+9; // get symbol '9'
//--- output values of constants
   printf("In a decimal form: symbol_0 = %d,  symbol_9 = %d",symbol_0,symbol_9);
   printf("In a hexadecimal form: symbol_0 = 0x%x,  symbol_9 = 0x%x",symbol_0,symbol_9);
//--- enter constants into a string
   string test="";
   StringSetCharacter(test,0,symbol_0);
   StringSetCharacter(test,1,symbol_9);
//--- this is what they look like in a string
   Print(test);
  }

反斜杠是编译器在处理程序源文本中的常量字符串和字符常量时使用的控制字符。一些符号,例如单引号(’)、双引号(")、反斜杠(\)和控制字符,可以根据以下表格中以反斜杠(\)开头的符号组合来表示:

字符名称助记符代码或图像MQL4中的记录数值
换行符(行进)LF‘\n’10
水平制表符HT‘\t’9
回车符CR‘\r’13
反斜杠\‘\\’92
单引号'‘\’’39
双引号"‘\"’34
十六进制代码hhhh‘\xhhhh’1到4个十六进制字符
十进制代码d‘\d’0到65535的十进制数

如果反斜杠后面跟着上述描述之外的字符,结果将是未定义的。

示例

void OnStart()
  {
//--- declare character constants
   int a='A';
   int b='$';
   int c='©';      // code 0xA9
   int d='\xAE';   // code of the symbol ®
//--- output print constants
   Print(a,b,c,d);
//--- add a character to the string
   string test="";
   StringSetCharacter(test,0,a);
   Print(test);
//--- replace a character in a string
   StringSetCharacter(test,0,b);
   Print(test);
//--- replace a character in a string
   StringSetCharacter(test,0,c);
   Print(test);
//--- replace a character in a string
   StringSetCharacter(test,0,d);
   Print(test);
//--- represent characters as a number
   int a1=65;
   int b1=36;
   int c1=169;
   int d1=174;
//--- add a character to the string
   StringSetCharacter(test,1,a1);
   Print(test);
//--- add a character to the string
   StringSetCharacter(test,1,b1);
   Print(test);
//--- add a character to the string
   StringSetCharacter(test,1,c1);
   Print(test);
//--- add a character to the string
   StringSetCharacter(test,1,d1);
   Print(test);
  }

如上所述,字符常量(或变量)的值是字符表中的索引。由于索引是整数,因此可以用不同的方式表示。

void OnStart()
  {
//---
   int a=0xAE;     // the code of ® corresponds to the '\xAE' literal
   int b=0x24;     // the code of $ corresponds to the '\x24' literal
   int c=0xA9;     // the code of © corresponds to the '\xA9' literal
   int d=0x263A;   // the code of ☺ corresponds to the '\x263A' literal
//--- show values
   Print(a,b,c,d);
//--- add a character to the string
   string test="";
   StringSetCharacter(test,0,a);
   Print(test);
//--- replace a character in a string
   StringSetCharacter(test,0,b);
   Print(test);
//--- replace a character in a string
   StringSetCharacter(test,0,c);
   Print(test);
//--- replace a character in a string
   StringSetCharacter(test,0,d);
   Print(test);
//--- codes of suits
   int a1=0x2660;
   int b1=0x2661;
   int c1=0x2662;
   int d1=0x2663;
//--- add a character of spades
   StringSetCharacter(test,1,a1);
   Print(test);
//--- add a character of hearts
   StringSetCharacter(test,2,b1);
   Print(test);
//--- add a character of diamonds
   StringSetCharacter(test,3,c1);
   Print(test);
//--- add a character of clubs
   StringSetCharacter(test,4,d1);
   Print(test);
//--- Example of character literals in a string
   test="Queen\x2660Ace\x2662";
   printf("%s",test);
  }

字符字面量的内部表示类型是ushort。字符常量可以接受0到65535的值。

另请参阅

StringSetCharacter()StringGetCharacter()ShortToString()ShortArrayToString()StringToShortArray()

最后更新于