MQL4字符串函数操作指南

StringConcatenate() – 字符串连接

StringFind() – 字符串搜索

StringGetChar() – 获取字符串中指定字符ASCII值

StringLen() – 获取字符串长度

StringSetChar() – 替换字符串中字符

StringSubstr() – 字符串截取

StringTrimLeft() – 删除字符串前字符

StringTrimRight() – 删除字符串后字符

StringConcatenate() – 字符串连接

string StringConcatenate(...)该函数用于生成字符串形式的数据并返回。参数可为任意类型,总数不超过64个。

按照 Print()、Alert() 和 Comment() 函数的规则,将参数转换为字符串,返回值是参数转换后连接起来的结果。

StringConcatenate() 函数相比使用加号运算符(+)连接字符串,运行速度更快且更节省内存。

参数:

... - 用逗号分隔所有字符串,最多64个参数。 示例:

string text; text=StringConcatenate("Account free margin is ", AccountFreeMargin(), "Current time is ", TimeToStr(TimeCurrent()));// 文本="Account free margin is " + AccountFreeMargin() + "Current time is " + TimeToStr(TimeCurrent()) Print(text);在tickmill外汇官网的交易环境中,字符串的高效处理能提升脚本执行效率。StringFind() – 字符串搜索

int StringFind(string text, string matched_text, void start)用于搜索子字符串。该函数返回子字符串在搜索字符串中的起始位置,若未找到则返回-1。

参数:

text - 被搜索的字符串。 matched_text - 需要搜索的字符串。 start - 搜索开始索引位置。 示例:

string text="快速的棕色小狗跨越过懒惰的狐狸"; int index=StringFind(text, "小狗跨越", 0); if(index!=16) Print("oops!"); StringGetChar() – 获取字符串中指定字符ASCII值

int StringGetChar(string text, int pos)返回字符串中指定位置的字符ASCII值。

参数:

text - 字符串。 pos - 字符串中字符位置,可从0至 StringLen(text)-1。 示例:

int char_code=StringGetChar("abcdefgh", 3); // 取出代码 'c' 是 99 StringLen() – 获取字符串长度

int StringLen(string text)返回字符串长度,即字符个数。

参数:

text - 字符串。示例:

string str="some text"; if(StringLen(str)<5) return(0);StringSetChar() – 替换字符串中字符

string StringSetChar(string text, int pos, int value)返回在指定位置替换过字符后的字符串。

参数:

text - 字符串。pos - 字符串中字符位置,可从0至 StringLen(text)-1。 value - 新字符的ASCII代码。 示例:

string str="abcdefgh"; string str1=StringSetChar(str, 3, 'D'); // str1 is "abcDefgh" StringSubstr() – 字符串截取

string StringSubstr(string text, int start, void length)从字符串指定位置开始截取子字符串。

若操作可行,此函数返回提取的子字符串;否则返回空字符串。

参数:

text - 字符串。start - 子字符串开始位置,可从0至 StringLen(text)-1。 length - 字符串截取长度,大于等于0;若参数未指定,则从给定位置截取至串尾。 示例:

string text="The quick brown dog jumps over the lazy fox"; string substr=StringSubstr(text, 4, 5); // 截取的字符串是"quick"单词 StringTrimLeft() – 删除字符串前字符

string StringTrimLeft(string text)本函数删除字符串左侧的回车符、空格和制表符。若成功,返回删除后的字符串;否则返回空字符串。

参数:

text - 字符串。 示例:

string str1=" Hello world "; string str2=StringTrimLeft(str); //str2将是 "Hello World " StringTrimRight() – 删除字符串后字符

string StringTrimRight(string text)本函数删除字符串右侧的回车符、空格和制表符。若成功,返回删除后的字符串;否则返回空字符串。

参数:

text - 字符串。 示例:

string str1=" Hello world "; string str2=StringTrimLeft(str); //str2将是 " Hello World"