标准C++ string处理增强函数

//标准C++ string 去除首尾空白字符 2012-8-12 
static inline void stringTrim(string &str)  
{  
    //去除左侧空白符    
    for (std::string::iterator iter = str.begin(); iter != str.end(); ++iter) {     
        if (!isspace(static_cast<unsigned char>(*iter))) {    
            str.erase(str.begin(), iter);    
            break;    
        }    
    }    
    //去除右侧空白符    
    for (std::string::reverse_iterator rev_iter = str.rbegin(); rev_iter != str.rend(); ++rev_iter) {     
        if (!isspace(static_cast<unsigned char>(*rev_iter))) {    
            str.erase(rev_iter.base(), str.end());  
            break;    
        }    
    }    
}  
  
//用分隔符将数组合并为字符串 2012-8-12 
//用法: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   
//用法:typedef vector<string> stringArray;  
void explode(const std::string &delimter, const std::string &str_source, stringArray *str_array)    
{    
    str_array->clear();    
    if (str_source.empty())    
        return;    
    std::string::size_type num_pos = 0, num_last_pos = 0;    
    num_pos = str_source.find(delimter);  
    //num_pos != num_last_pos防止delimter为空字符时死循环  
    while (std::string::npos != num_pos && num_pos != num_last_pos) {    
        str_array->push_back(str_source.substr(num_last_pos, num_pos - num_last_pos));  
        num_last_pos = num_pos + delimter.length();    
        num_pos = str_source.find(delimter, num_last_pos);  
    }   
    str_array->push_back(str_source.substr(num_last_pos));   
}  
  
//标准C++ std::string 仿CString 替换字符串 
//用法: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 截取指字区域内字符串 
//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++ 截取指字区域内字符串   
#include <string>  
/* 
 *功能:截取字符串的指定范围内的子串 
 *参数:strSrc源字符串,strStart开始字符,strEnd结束字符,keepStart是否保留开始字符,keepEnd是否保留结束字符 
 */  
std::string substr(const std::string &strSrc, const std::string &strStart, const std::string &strEnd, bool keepStart = false, bool keepEnd = false)    
{    
    if (strStart.empty() || strEnd.empty()) {  
        return strSrc;  
    }  
    std::string::size_type iStart = 0, iEnd = 0;    
    std::string sSub = "";    
    iStart = strSrc.find(strStart);   
    if (std::string::npos != iStart) {    
        if (keepStart)   
            sSub =  strSrc.substr(iStart);    
        else  
            sSub =  strSrc.substr(iStart + strStart.length());    
          
        iEnd = sSub.find(strEnd);    
        if (std::string::npos != iEnd) {    
            if (keepEnd)  
                return sSub.substr(0, iEnd + strEnd.length());  
            else  
                return sSub.substr(0, iEnd);  
        }    
    }    
    return sSub;    
}  
  
//标准C++ 无返回值 截取指字区域内字符串 
#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());  
        }  
    }  
}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值