这个版本的explode 内存不是动态分配的,而且,有一些bug,请不要使用了。抱歉一个。
MQL4 在处理金融数据时候,经常要进行字符串的替换,和 csv 格式的解析。 下面是两个比较常用的函数,一个是把一个字符串转换成一个数组。一个是进行字符串替换。 比如,要解析一个csv格式的文件,可以先读出这个文件,然后,用 "\n" 进行 explode 然后,对每一行用 "," 进行explode,就完成了一个 csv的解析。 字符串替换,就更加常见了,这里就不多啰嗦了。给代码:
- int explode(string delimiter , string str, string &buff[])
- {
- int start = 0, index, i = 0, delimiter_len;
- delimiter_len = StringLen(delimiter);
- index = StringFind(str, delimiter, start);
-
- while (index != -1)
- {
- buff[i] = StringSubstr(str, start, index - start);
- start = index + delimiter_len;
- index = StringFind(str, delimiter, start);
- i++;
- }
- if (i > 0) i--;
- if (index == -1) {
- index = StringLen(str);
- if (start < index) {
- i++;
- buff[i] = StringSubstr(str, start, index - start);
- }
- }
- return (i + 1);
- }
- string str_replace(string search, string replace, string str)
- {
- int start = 0, index, i = 0, search_len, replace_len;
- search_len = StringLen(search);
- replace_len = StringLen(replace);
- index = StringFind(str, search, start);
- while (index != -1)
- {
- str = StringSubstr(str, start, index) + replace + StringSubstr(str, index + search_len);
- index = StringFind(str, search, index + replace_len);
- }
- return (str);
- }
复制代码
|