zoukankan      html  css  js  c++  java
  • 再加几个MQL4 中的字符串处理函数

    前面文章 http://www.cnblogs.com/niniwzw/archive/2009/12/01/1614784.html 中的字符串处理函数有bug,请使用新版本的函数。

    1. 已经把explode 函数改成动态内存分配了,只要你 这样定义buff就可以了 string buff[];

    2. 新加了,implode 函数,把数组返回成字符串,和 explode 相反。

    3. toLower 和 toUpper 是大小写转换的。

    4. 本人的一点心得: 字符串处理因为会有很多特殊的情况,要写的完全没有bug,可能还是有点难度的。

    explode 的函数,可以比较一下我原来的版本,会发现,有些bug不仔细测试,还可能很难发现。

    5. 网上有很多这些版本的函数, 也可以下载下来看看,会发现bug 很多,很多脚本没有考虑中文的情况。

    代码
    /**
    * 把一个字符串,切分到一个数组里面去,用于csv格式的解析
    */
    int explode(string delimiter , string str, string &buff[])
    {
        
    int start = 0, index, i = 0, delimiter_len,  max  = 0, step = 20;
        delimiter_len 
    = StringLen(delimiter);
        index 
    = StringFind(str, delimiter, start);
        max 
    = ArraySize(buff);
        
    while (index != -1)
        {
            
    if (i >= max) {
                max 
    += step;
                ArrayResize(buff, max);
            }
            
    //Alert("index : " + index , "start : " + start);
            if (index - start > 0) {
                buff[i] 
    = StringSubstr(str, start, index - start);
            } 
    else {
                buff[i] 
    = "";
            }
            start 
    = index + delimiter_len;
            index 
    = StringFind(str, delimiter, start);
            i
    ++;
        }
        
    if (index == -1) {
            index 
    = StringLen(str);
            
    if (start < index) {
                
    if (i >= max) {
                    max 
    += 1;
                    ArrayResize(buff, max);
                }
                buff[i] 
    = StringSubstr(str, start, index - start);
            } 
    else {
                
    if (i > 0) i--;
            }
        }
        
    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);
    }


    string implode(string sDelimiter, string sImplode[]){
       
       
    string slImplode = "";
       
    for (int i = 0; i < ArraySize(sImplode); i++) {
          slImplode 
    = StringConcatenate(slImplode, sImplode[i], sDelimiter);
       }
       
    return(StringSubstr(slImplode, 0, (StringLen(slImplode) - StringLen(sDelimiter))));
    }


    string toLower(string sText) {
       
    int i = 0, size;
       size 
    = StringLen(sText);
       
    while (i < size){
          
    int ilChar = StringGetChar(sText, i);
          
    if (ilChar >= 65 && ilChar <= 90) {
             sText 
    = StringSetChar(sText, i, ilChar+32);
          }
          i
    ++;
       }
       
    return(sText);
    }

    string toUpper(string sText){
       
       
    int i = 0, size;
       size 
    = StringLen(sText);
       
    while (i < size){
          
    int ilChar = StringGetChar(sText, i);
          Alert(ilChar);
          
    if (ilChar >= 97 && ilChar <= 122) {
             sText 
    = StringSetChar(sText, i, ilChar-32);
          }
          i
    ++;
       }
       
    return(sText);
    }

     

  • 相关阅读:
    MFC关闭子窗口 如何把父窗口也一起关闭
    VS2010创建动态链接库并且使用动态链接库DLL
    Linux文件处理命令
    Centos ftp服务器安装配置
    PHP数字价格格式化,保留两位小数
    PHP中file_put_contents追加和换行
    如何查看Laravel版本号的三种方法
    性能优化系列一:性能优化介绍与优化的范围
    只需两步获取任何微信小程序源码
    前端网页、php与mysql数据库字符编码(解决中文等乱码问题
  • 原文地址:https://www.cnblogs.com/niniwzw/p/1616455.html
Copyright © 2011-2022 走看看