为什么std::string 连最常用,最基本的功能也没有呢?简直是让人无语!
标准C++ 字符串处理增强函数:
//标准C++ string 去除首尾空白字符 2012-8-12 By Dewei
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 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 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 替换字符串 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.youkuaiyun.com/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>
/*
*功能:截取字符串的指定范围内的子串
*参数: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++ 无返回值 截取指字区域内字符串 2012-6-23 By Dewei
#include <string>
using std::string;
//string strSrc("http://download.youkuaiyun.com/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());
}
}
}