标准C++ 字符串处理增强函数

转自:http://dewei.iteye.com/blog/1566734

//标准C++ string 去除首尾空白字符 2012-8-12 By Dewei
static inline void stringTrim(string &str)
{
    //去除左侧空白符
    for (int i = 0; i != str.length(); ++i) {
        char tmp = str[i];
        if (!isspace(tmp)) {
            str = str.c_str() + i;
            break;
        }
    }
    //去除右侧空白符
    for (int i = str.length() - 1; i != 0; --i) {
        char tmp = str[i];
        if (!isspace(tmp)) {
            str.resize(i+1);
            break;
        }
    }
}

//用分隔符将数组合并为字符串 2012-8-12 by Dewei 
//用法:typedef vector<string> stringArray;
string implode(string delimter, stringArray& str_array)
{
    string str;
    int num_count = str_array.size();
    for (int i = 0; i < num_count; ++i) {
        if (!str_array[i].empty())
            str.append(str_array[i]);
        if (i < num_count -1)
            str.append(delimter);
    }
    return str;
}
//将字符串转换成数组(支持值为空) 2012-8-12 by Dewei 
//用法:typedef vector<string> stringArray;
void explode(const string &delimter, string str_source, stringArray &str_array)
{
    str_array.clear();
    if (delimter.empty() || str_source.empty())
        return;
    string str_tmp;
    string::size_type num_last_pos = 0;
    string::size_type num_pos = 0;
    while (num_pos != string::npos) {
        num_pos = str_source.find(delimter, num_last_pos); 
        if(num_pos != string::npos)  { 
            str_tmp = str_source.substr(num_last_pos, num_pos - num_last_pos);
            num_last_pos = num_pos + delimter.length();
        } 
        else  { 
            str_tmp = str_source.substr(num_last_pos, str_source.length() - num_last_pos);
        } 
        stringTrim(str_tmp);
        str_array.push_back(str_tmp);
    }
}

//标准C++ std::string 仿CString 替换字符串 by Dewei 2012-6-24
//用法:using namespace std;
//source_str = str_replace(oldstr, newstr, source_str);
static inline string str_replace(const string oldstr, const string newstr, string source_str)
{
    string::size_type num_pos = 0;
    num_pos = source_str.find(oldstr);
    while (num_pos != string::npos) {
        source_str.replace(num_pos, oldstr.length(), newstr);
        num_pos = source_str.find(oldstr, num_pos+oldstr.length());
    }
    return source_str;
}
//CString 截取指字区域内字符串 2012-6-6 By Dewei
//CString strSrc(_T("http://download.csdn.net/download/lindao0/242800"));
//CString strNew;    
//strNew = substr(strSrc, "//", "/");
//

CString substr(CString strSrc, const CString strStart, const CString strEnd)
{
    int iStart = 0, iEnd = 0;
    CString sSub = "";
    iStart = strSrc.Find(strStart) + lstrlen(strStart) ;
    if (iStart != -1) {
        sSub =    strSrc.Mid(iStart);
        iEnd = sSub.Find(strEnd);
        if (iEnd != -1)
        {
            sSub = sSub.Left(iEnd);
        }
    }
    return sSub;
}

//标准C++ 截取指字区域内字符串 2012-6-23 By Dewei
#include <string>
using std::string;

//string strSrc("http://download.csdn.net/download/lindao0/242800");
//string strNew;    
//strNew = substr(strSrc, "//", "/");
//printf("%s", strNew.c_str());

string substr(string strSrc, const string strStart, const string strEnd)
{
    int iStart = 0, iEnd = 0;
    string sSub = "";
    iStart = strSrc.find(strStart) + strStart.size();
    if (iStart != -1) {
        sSub =    strSrc.substr(iStart);
        iEnd = sSub.find(strEnd);
        if (iEnd != -1) {
            return sSub.substr(0, iEnd);
        }
    }
    return sSub;
}

//标准C++ 无返回值 截取指字区域内字符串 2012-6-23 By Dewei
#include <string>
using std::string;

//string strSrc("http://download.csdn.net/download/lindao0/242800");
//char out[1024] = {0};    
//substr(strSrc, "//", "/", out);
//printf("%s", out);
void substr(string &strSrc, const string &strStart, const string &strEnd, char *out)
{
    int iStart = 0, iEnd = 0;
    string sSub = "";
    iStart = strSrc.find(strStart) + strStart.size();
    if (iStart != -1) {
        sSub =    strSrc.substr(iStart);
        iEnd = sSub.find(strEnd);
        if (iEnd != -1) {
            sSub = sSub.substr(0, iEnd);
            strcpy(out, sSub.c_str());
        }
    }
}
原文地址:https://www.cnblogs.com/sevenyuan/p/3256148.html