EA编程字符函数使用教程详解
本文介绍用于处理字符串类型数据的一系列函数,这些函数在EA编程中非常实用。
StringConcatenate
StringFind
StringGetChar
StringLen
StringSetChar
StringSubstr
StringTrimLeft
StringTrimRight
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);
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!");
int StringGetChar( string text, int pos)
返回字符串指定位置字符的ASCII码。
参数说明:
text - 源字符串。
pos - 要获取字符的位置索引,范围从0到StringLen(text)-1。
应用示例:
int char_code=StringGetChar("abcdefgh", 3);
// 取出的字符'c'对应的ASCII码是99
int StringLen( string text)
返回字符串中包含的字符数量。
参数说明:
text - 需要计算长度的字符串。
应用示例:
string str="some text";
if(StringLen(str)<5) return(0);
string StringSetChar( string text, int pos, int value)
返回一个字符串副本,其中指定位置的字符被替换为新字符。
参数说明:
text - 需要进行字符替换的字符串。
pos - 字符串中字符的位置索引,范围从0到StringLen(text)。
value - 新的ASCII码值。
应用示例:
string str="abcdefgh";
string str1=StringSetChar(str, 3, 'D');
// str1的结果是"abcDefgh"
string StringSubstr( string text, int start, void length)
从指定起始位置提取源字符串中的一部分。
如果提取成功,函数返回子字符串的副本,否则返回空字符串。
参数说明:
text - 将被提取的源字符串。
start - 子字符串的起始索引,范围从0到StringLen(text)-1。
length - 要提取的子字符串长度。如果该参数值大于等于0或未指定,则按规则提取。
应用示例:
string text="快速的棕色小狗跨越过懒惰的狐狸";
string substr=StringSubstr(text, 4, 5);
// 提取出的子字符串是"快速"这个词
string StringTrimLeft( string text)
删除字符串左侧的空格和制表符。如果操作成功,函数返回处理后的副本,否则返回空字符串。
参数说明:
text - 需要删除左侧空格的字符串。
应用示例:
string str1=" Hello world ";
string str2=StringTrimLeft(str1);
// 处理后的str2结果是"Hello World "
string StringTrimRight( string text)
删除字符串右侧的空格和制表符。如果操作成功,函数返回处理后的副本,否则返回空字符串。
参数说明:
text - 需要删除右侧空格的字符串。
应用示例:
string str1=" Hello world ";
string str2=StringTrimRight(str1);
// 处理后的str2结果是" Hello World"
掌握这些字符串处理函数,对于在IG外汇平台等交易环境下的EA编程和数据处理至关重要,能有效提升自动化交易策略的开发效率。
上一篇:外汇交易中如何有效运用止损策略
下一篇:没有了
