zoukankan      html  css  js  c++  java
  • 字符串替换函数

    const int MAX_STRLEN = 256;
    //---------------------------------------------------------------------------

    // Name : strrep
    // Desc : Replace all matched substring.
    // Param :
    // str : Null-terminated string to replace.
    // match : Null-terminated string to match for.
    // rep : string to replace for.
    // return : 0 Success,-1 Fail.
    int strrep(char *str, const char *match, const char *rep)
    {
    if (!str || !match)
    return -1;
    char tmpstr[MAX_STRLEN];
    char *pos = strstr(str, match);
    while (pos)
    {
    memset(tmpstr, 0, sizeof(tmpstr));
    strncpy(tmpstr, str, pos - str);
    strcat(tmpstr, rep);
    strcat(tmpstr, pos + strlen(match));
    strcpy(str, tmpstr);
    pos = strstr(str, match);
    }
    return 0;
    }
    //---------------------------------------------------------------------------

    // Name : wcsrep
    // Desc : Replace all matched substring.
    // Param :
    // str : Null-terminated string to replace.
    // match : Null-terminated string to match for.
    // rep : string to replace for.
    // return : 0 Success,-1 Fail.
    int wcsrep(wchar_t *str, const wchar_t *match, const wchar_t *rep)
    {
    if (!str || !match)
    return -1;
    wchar_t tmpstr[MAX_STRLEN];
    wchar_t *pos = wcsstr(str, match);
    while (pos)
    {
    memset(tmpstr, 0, sizeof(tmpstr));
    wcsncpy(tmpstr, str, pos - str);
    wcscat(tmpstr, rep);
    wcscat(tmpstr, pos + wcslen(match));
    wcscpy(str, tmpstr);
    pos = wcsstr(str, match);
    }
    return 0;
    }
    //---------------------------------------------------------------------------
  • 相关阅读:
    [译] 第二十天:Stanford CoreNLP
    [译] 第十九天: Ember
    [译] 第十八天:BoilerPipe
    [译] 第十七天:JBoss Forge
    [译] 第十六天: Goose Extractor
    [译] 第十五天:Meteor
    [译] 第十四天:Standford NER
    [译] 第十三天:Dropwizard
    [译] 第十二天: OpenCV
    hadoop-MR
  • 原文地址:https://www.cnblogs.com/plus/p/2363086.html
Copyright © 2011-2022 走看看