zoukankan      html  css  js  c++  java
  • 提供MQL4 中比较实用的两个字符串处理函数

    这篇文章已经有了新版本:

    再加几个MQL4 中的字符串处理函数

    这个版本的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);
    }

    代码很简单,也不做说明了,关键是很实用。

  • 相关阅读:
    导入已有项目到svn
    Linux用ICMP协议实现简单Ping网络监测功能
    c++ tcp 服务器和客户端例子
    C++ Socket 编程
    c++工厂模式和多线程结合
    Linux中ls命令详解
    Mac OS X 11中的/usr/bin 的“Operation not permitted”
    Warning: mysql_connect(): No such file or directory 解决方案总结(操作系统: Mac)
    页面组件渲染小组件(重点)
    Vue 路由
  • 原文地址:https://www.cnblogs.com/niniwzw/p/1614784.html
Copyright © 2011-2022 走看看