柯尔凯思:MQL4字符串处理函数

2025-07-21

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);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)-l。 示例:

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"

版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权本站发表,未经许可,不得转载。

下一篇 : 没有了