字符串处理的封装,这里做备份,该段代码仍然有许多地方不完善,待日后修缮
这是.h头文件:
#include <string>
#include <vector>
#include <algorithm>
#include <tchar.h>
#include <Windows.h>
#include <stdarg.h>
using namespace std;
class StringUtil
{
public:
StringUtil();
~StringUtil();
//分割字符串,如strtok,返回到容器token中,單個字符分割
void SplitByChar (vector<string>& tokens,const string& str,const string& delimiters = " " );
void SplitByStr (vector<string>& temp,const string& strSour,const string& Strdeli = " " );
//去掉空格,去掉所有空格,去掉最左邊和最右邊的空格,去電左邊第一個空格,去掉右邊第一個空格
static string TrimAll (string &str, const string& delims = " \t\n\r");
static string Trim (string &str, const string& delims = " \t\n\r", bool left = true, bool right = true );
static void TrimLeft (string &str, const string& delims = " \t\n\r");
static void TrimRight (string &str, const string& delims = " \t\n\r");
//轉化
string TransfIntToStr ( int val );
string TransfDoubleToStr ( double val );
// static string StrUp(const string& strSrc);
// static string StrLo(const string& strSrc);
//截取字符串strStart,字符串strEnd之間的字符串.默認情況是第一個找到的字符串
static string GetStrByMultistr (const string& strSrc,const string& strStart,const string& strEnd = "",size_t foundStart=1,size_t foundEnd=1,bool blErase = true);
//截取字符串delims右邊的字符串信息或者字符串delims左邊的字符串信息,不包含包含strStart字符串
static string GetStrLeft (const string& strSrc,const string& strStart,bool blErase = true);
static string GetStrRight (const string& strSrc,const string& strStart,bool blErase = true);
//截取第foundStart到foundEnd之間的字符串
static string GetStrByPosi (const string& strSrc,size_t foundStart,size_t foundEnd,bool blErase = true);
//截取第found位置左邊的字符串或者右邊的字符串
static string GetStrLeftPosi (const string& strSrc,size_t found,bool blErase = true);
static string GetStrRightPosi (const string& strSrc,size_t found,bool blErase = true);
//拼接字符串strStart到strsrc到最前邊或者最後邊
// static string LeftStrcat(const string& strSrc,const string& strStart,bool blErase = true);
// static string RightStrcat(const string& strSrc,const string& strEnd,bool blErase = true);
//在posi位置拼接字符串str到strsrc字符串中
static string AppendMidPosi (const string& strSrc,size_t posi,const string& str,bool blErase = true);
//在字符串strfront之前拼接str到strsrc字符串中
static string AppendMidStrLeft (const string& strSrc,const string& strFront,const string& str,bool blErase = true);
static string AppendMidStrRright (const string& strSrc,const string& strRear,const string& str,bool blErase = true);
//在第posi個字符串strfront之前拼接str到strsrc字符串中
static string AppendMultiStrLeft (const string& strSrc,const string& strFront,size_t posi,const string& str,bool blErase = true);
static string AppendMultiStrRright (const string& strSrc,const string& strRear,size_t posi,const string& str,bool blErase = true);
string TransfStrUpper (const string& str);//转换为大写
string TransfStrLower (const string& str);//转换为小写
string AppendLeft (const string& str,const string& strLeft);//加在左边
string AppendRight (const string& str,const string& strRight);//加在右边
string AppendChecking (size_t number,const string& str,const string& checkString,string strCat,...);//右边连接strCat,直到出现checkString为止,number为strCat数目
//取第n行,number小于1,返回原字符串,number超过行数,返回最后一行
string GetLine (const string& str,size_t number = 1);
//取第number次,含有字符串checkString的行
string GetLineOfChecking (const string& str,const string& checkString,size_t number = 1);
//删除字符/字符串checkString
string EraseStr (const string& str,const string& checkString);
//截取字符串delims右邊的字符串信息或者字符串delims左邊的字符串信息,其中包含delims字符串,且未去掉空格
string EraseLeft (const string &str,const std::string& delims,bool blOneself = false);
string EraseRight (const string &str,const std::string& delims,bool blOneself = false);
};
这是.cpp文件
//#include "stdafx.h"
#include "stringdeal.h"
#include <sstream>
#include <vector>
using namespace std;
StringUtil::StringUtil()
{}
StringUtil::~StringUtil()
{}
//按照delimiters分割字符串str,并保存到tokens中
void StringUtil::SplitByChar( vector<string>& tokens ,
const string& str,
const string& delimiters)
{
tokens.clear();
// Skip delimiters at beginning.
string::size_type lastPos = str.find_first_not_of( delimiters, 0 );
// Find first "non-delimiter".
string::size_type pos = str.find_first_of( delimiters, lastPos );
while( string::npos != pos || string::npos != lastPos )
{
// Found a token, add it to the vector.
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the "not_of"
lastPos = str.find_first_not_of(delimiters, pos);
// Find next "non-delimiter"
pos = str.find_first_of(delimiters, lastPos);
}
}
//按照delimiters分割字符串str,并保存到tokens中
void StringUtil::SplitByStr( vector<string>& temp ,
const string& strSour,
const string& Strdeli)
{
temp.clear();
// Skip delimiters at beginning.
string::size_type pos = strSour.find( Strdeli, 0 );
string::size_type Startpos =0;
while( string::npos != pos )
{
// Found a token, add it to the vector.
temp.push_back(strSour.substr(Startpos, pos-Startpos));
Startpos=pos+Strdeli.length();
if ((pos+Strdeli.length())!=strSour.length())
{
pos = strSour.find(Strdeli, pos+Strdeli.length());
if (string::npos == pos)
{
temp.push_back(strSour.substr(Startpos, pos-Startpos));
}
}
}
}
//去除全部的空格,包含" \t\r\n"
string StringUtil::TrimAll( string &str,
const string& delims /*default:delims = " \t\n\r"*/
)
{
//string szTemp=str;
std::size_t found = -1;
//删除空格
found = str.find(' ');
while(found != string::npos)
{
str.erase(found,1);
found = str.find(' ');
}
//删除\t
found = str.find('\t');
while(found != string::npos)
{
str.erase(found,1);
found = str.find('\t');
}
//删除\r
found = str.find('\r');
while(found != string::npos)
{
str.erase(found,1);
found = str.find('\r');
}
//删除\n
found = str.find('\n');
while(found != string::npos)
{
str.erase(found,1);
found = str.find('\n');
}
return str;
}
//去除左邊和右邊的第一個空格
string StringUtil::Trim( string &str,
const string& delims,
bool left,
bool right )
{
if(left)
{
TrimLeft( str, delims );
}
if(right)
{
TrimRight( str, delims );
}
return str;
}
void StringUtil::TrimLeft( string &str, const string& delims /*default:delims = " \t\n\r"*/)
{
str.erase( 0, str.find_first_not_of( delims ));
}
void StringUtil::TrimRight( string &str, const string& delims/*default:delims = " \t\n\r"*/ )
{
str.erase( str.find_last_not_of( delims ) + 1 );
}
//將int型轉化為string
string StringUtil::TransfIntToStr( int val )
{
std::stringstream out;
out << val;
return out.str();
}
//將double型轉化為string
string StringUtil::TransfDoubleToStr( double val )
{
std::stringstream out;
out << val;
return out.str();
}
//截取字符串delims右邊的字符串信息,并返回str
string StringUtil::EraseLeft(const string &str,const string& delims,bool blOneself)
{
string tmpsrc=str;
std::size_t found = -1;
found = tmpsrc.find( delims );
if(found != string::npos)
{
if(blOneself)
tmpsrc.erase( 0,found + delims.length());
else
tmpsrc.erase(0,found);
}
return tmpsrc;
}
//截取字符串delims左邊的字符串信息,并返回str
string StringUtil::EraseRight(const string &str,const string& delims,bool blOneself)
{
string tmpsrc=str;
std::size_t found = -1;
found = tmpsrc.find( delims );
if(found != string::npos)
{
if(blOneself)
tmpsrc.erase(found,string::npos);
else
tmpsrc.erase(found + delims.length(),string::npos);
}
return tmpsrc;
}
//截取字符串strStart,字符串strEnd之間的字符串.默認情況是第一個找到的字符串
string StringUtil::GetStrByMultistr(const string& strSrc,const string& strStart,const string& strEnd,size_t foundS,size_t foundE,bool blErase)
{
string tmpStrSrc = strSrc;
string tmpStrStart = strStart;
string tmpStrEnd = strEnd;
//記錄字符串中出現子字符串的次數
std::size_t foStarNum = 0;
std::size_t foEndNum = 0;
string retStr;
if(blErase)
{
tmpStrSrc=TrimAll(tmpStrSrc);
}
//獲取第一個字符串的位置
std::size_t foundStart = tmpStrSrc.find(tmpStrStart);
while (foundStart!=string::npos)
{
foStarNum+=1;
if (foStarNum!=foundS)
{
foundStart = tmpStrSrc.find(tmpStrStart,foundStart+tmpStrStart.length());
}
else
{
break;
}
}
//獲取結尾字符串的位置
std::size_t foundEnd = tmpStrSrc.find(strEnd);
while (foundEnd!=string::npos)
{
foEndNum+=1;
if (foEndNum!=foundE)
{
foundEnd=tmpStrSrc.find(tmpStrEnd,foundEnd+tmpStrEnd.length());
}
else
{
break;
}
}
if (foundStart==-1||foundEnd==-1)
{
retStr = "No Found";
return retStr;
}
if(foundEnd == 0||foundStart==foundEnd)
retStr = tmpStrSrc.substr(foundStart + tmpStrStart.length());
else
retStr = tmpStrSrc.substr(foundStart + tmpStrStart.length(), foundEnd - foundStart - tmpStrStart.length());
// std::size_t foundStart = tmpStrSrc.find(tmpStrStart);
// std::size_t foundEnd = tmpStrSrc.find(strEnd);
//
// if(foundStart == -1 || foundEnd == -1)
// {
// retStr = "No Found";
// return retStr;
// }
//
// if(foundEnd == 0)
// retStr = tmpStrSrc.substr(foundStart + tmpStrStart.length());
// else
// retStr = tmpStrSrc.substr(foundStart + tmpStrStart.length(), foundEnd - foundStart - tmpStrStart.length());
// if(!blErase)
// {
// TrimAll(retStr);
// }
return retStr;
}
//截取字符串strstart左邊的字符串信息,不包括strstart
string StringUtil::GetStrLeft(const string& strSrc,const string& strStart,bool blErase)
{
string tmpStrSrc = strSrc;
string tmpStrStart = strStart;
string retStr;
if(blErase)
{
tmpStrSrc=TrimAll(tmpStrSrc);
}
std::size_t foundStart = tmpStrSrc.find(tmpStrStart);
if(foundStart == -1)
{
retStr = "No Found";
return retStr;
}
if(foundStart == 0)
retStr = "";
else
retStr = tmpStrSrc.substr(0, foundStart);
if(!blErase)
{
TrimAll(retStr);
}
return retStr;
}
//截取字符串strstart右邊的字符串信息(不包括字符串strstart)
string StringUtil::GetStrRight(const string& strSrc,const string& strStart,bool blErase)
{
string tmpStrSrc = strSrc;
string tmpStrStart = strStart;
string retStr;
if(blErase)
{
tmpStrSrc=TrimAll(tmpStrSrc);
}
std::size_t foundStart = tmpStrSrc.find(tmpStrStart);
if(foundStart == -1)
{
retStr = "No Found";
return retStr;
}
if(foundStart ==tmpStrSrc.length())
retStr = "";
else
retStr = tmpStrSrc.substr(foundStart+tmpStrStart.length(),strSrc.length()-foundStart-tmpStrStart.length());
if(!blErase)
{
TrimAll(retStr);
}
return retStr;
}
//截取從foundstart到foundend之間的字符串信息
string StringUtil::GetStrByPosi(const string& strSrc,size_t foundStart,size_t foundEnd,bool blErase)
{
string tmpStrSrc = strSrc;
string retStr;
if(blErase)
{//删除空格,包含(" \t\r\n")
tmpStrSrc=TrimAll(tmpStrSrc);
}
if(foundEnd == 0)
retStr = tmpStrSrc.substr(foundStart);
else
retStr = tmpStrSrc.substr(foundStart, foundEnd - foundStart);
if(!blErase)
{
TrimAll(retStr);
}
return retStr;
}
//截取第found位置左邊的字符串
string StringUtil::GetStrLeftPosi(const string& strSrc,size_t found,bool blErase)
{
string tmpStrSrc = strSrc;
string retStr;
if(blErase)
{//删除空格,包含(" \t\r\n")
tmpStrSrc=TrimAll(tmpStrSrc);
}
if(found == 0)
retStr ="";
else
retStr = tmpStrSrc.substr(0,found);
if(!blErase)
{
TrimAll(retStr);
}
return retStr;
}
//截取第found位置右邊的字符串
string StringUtil::GetStrRightPosi(const string& strSrc,size_t found,bool blErase)
{
string tmpStrSrc = strSrc;
string retStr;
if(blErase)
{//删除空格,包含(" \t\r\n")
tmpStrSrc=TrimAll(tmpStrSrc);
}
if(found==tmpStrSrc.length())
retStr ="";
else
retStr = tmpStrSrc.substr(found,tmpStrSrc.length()-found);
if(!blErase)
{
TrimAll(retStr);
}
return retStr;
}
//將字符串連接在當前字符指定的位置處//在posi位置拼接字符串str到strsrc字符串中
string StringUtil::AppendMidPosi(const string& strSrc,size_t posi,const string& str,bool blErase)
{
string tmpStrSrc = strSrc;
string tmpStr = str;
string retStr;
if(blErase)
{
tmpStrSrc=TrimAll(tmpStrSrc);
}
//append會修改之前的字符串
if (tmpStrSrc.length()==0)
{//assign賦值
retStr=tmpStrSrc.assign(tmpStr);
}
else
{
retStr=tmpStrSrc.insert(posi,tmpStr);
}
if(!blErase)
{
TrimAll(retStr);
}
return retStr;
}
//在字符串strFront之前插入字符串str //在字符串strfront之前拼接str到strsrc字符串中
string StringUtil::AppendMidStrLeft(const string& strSrc,const string& strFront,const string& str,bool blErase)
{
string tmpStrSrc = strSrc;
string tmpStrFront=strFront;
string tmpStr = str;
string retStr;
if(blErase)
{
tmpStrSrc=TrimAll(tmpStrSrc);
}
if (tmpStrSrc.find(tmpStrFront)!=-1)
{
retStr=tmpStrSrc.insert(tmpStrSrc.find(tmpStrFront),tmpStr);
}
else
{
retStr=tmpStrSrc.insert(0,tmpStr);
}
if(!blErase)
{
TrimAll(retStr);
}
return retStr;
}
//在字符串strRear之后插入字符串str //在字符串strRear之后拼接str到strsrc字符串中
string StringUtil::AppendMidStrRright(const string& strSrc,const string& strRear,const string& str,bool blErase)
{
string tmpStrSrc = strSrc;
string tmpStrRear=strRear;
string tmpStr = str;
string retStr;
if(blErase)
{
tmpStrSrc=TrimAll(tmpStrSrc);
}
if (tmpStrSrc.find(tmpStrRear)!=-1)
{
retStr=tmpStrSrc.insert(tmpStrSrc.find(tmpStrRear)+tmpStrRear.length(),tmpStr);
}
else
{
retStr=tmpStrSrc.insert(0,tmpStr);
}
if(!blErase)
{
TrimAll(retStr);
}
return retStr;
}
//在第幾個字符串strRear之后插入字符串str //在第posi個字符串strfront之前拼接str到strsrc字符串中
string StringUtil::AppendMultiStrRright(const string& strSrc,const string& strRear,size_t posi,const string& str,bool blErase)
{
string tmpStrSrc = strSrc;
string tmpStrRear=strRear;
string tmpStr = str;
string retStr;
if(blErase)
{
tmpStrSrc=TrimAll(tmpStrSrc);
}
std::size_t found = -1;
std::size_t foundNum = 0;
//删除空格
found = tmpStrSrc.find(tmpStrRear);
while(found != string::npos)
{
foundNum+=1;
if (foundNum==posi)
{
retStr=tmpStrSrc.insert(found+tmpStrRear.length(),tmpStr);
break;
}
found = tmpStrSrc.find(tmpStrRear,found+tmpStrRear.length());
}
if(!blErase)
{
TrimAll(retStr);
}
return retStr;
}
//在(第幾個)字符串(strFront)之前插入字符串str //在第posi個字符串strfront之后拼接str到strsrc字符串中
string StringUtil::AppendMultiStrLeft(const string& strSrc,const string& strFront,size_t posi,const string& str,bool blErase)
{
string tmpStrSrc = strSrc;
string tmpStrFront=strFront;
string tmpStr = str;
string retStr;
if(blErase)
{
tmpStrSrc=TrimAll(tmpStrSrc);
}
std::size_t found = -1;
std::size_t foundNum = 0;
//删除空格
found = tmpStrSrc.find(tmpStrFront);
while(found != string::npos)
{
foundNum+=1;
if (foundNum==posi)
{
retStr=tmpStrSrc.insert(found,tmpStr);
break;
}
found = tmpStrSrc.find(tmpStrFront,found+tmpStrFront.length());
}
if(!blErase)
{
TrimAll(retStr);
}
return retStr;
}
//轉化為大寫
// string StringUtil::StrUp(const string& strSrc)
// {
// string tmpStr=strSrc;
// string upStr="abcdefghijklmnopqrstuvwxyz";
//
// string::size_type pos = tmpStr.find_first_of( upStr,0);
// while( string::npos != pos)
// {
// tmpStr.replace(pos,1,1,tmpStr[pos]-32);
// pos = tmpStr.find_first_of(upStr, pos);
// }
//
// return tmpStr;
// }
// 轉化為小寫
// string StringUtil::StrLo(const string& strSrc)
// {
// string tmpStr=strSrc;
// string loStr="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//
// string::size_type pos = tmpStr.find_first_of( loStr,0);
// while( string::npos != pos)
// {
// tmpStr.replace(pos,1,1,tmpStr[pos]+32);
// pos = tmpStr.find_first_of(loStr, pos);
// }
// return tmpStr;
// }
string StringUtil::TransfStrUpper(const string& str)
{
string strTemp=str;
transform(strTemp.begin(),strTemp.end(),strTemp.begin(),::toupper);
return strTemp;
}
string StringUtil::TransfStrLower(const string& str)
{
string strTemp=str;
transform(strTemp.begin(),strTemp.end(),strTemp.begin(),::tolower);
return strTemp;
}
//將字符串連接在當前字符串的最前邊//拼接字符串strStart到strsrc到最前邊
// string StringUtil::LeftStrcat(const string& strSrc,const string& strStart,bool blErase)
// {
// string tmpStrSrc = strSrc;
// string tmpStrStart = strStart;
//
// string retStr;
//
// if(blErase)
// {
// tmpStrSrc=AllTrim(tmpStrSrc);
// }
// //append會修改之前的字符串
// if (tmpStrSrc.length()==0)
// {
// retStr=tmpStrSrc.assign(tmpStrStart);
// }
// else
// {
// retStr=tmpStrStart.append(tmpStrSrc);
// }
// if(!blErase)
// {
// AllTrim(retStr);
// }
// return retStr;
//
// }
// //將字符串連接在當前字符串的後邊//拼接字符串strStart到strsrc到最後邊
// string StringUtil::RightStrcat(const string& strSrc,const string& strEnd,bool blErase)
// {
// string tmpStrSrc = strSrc;
// string tmpStrEnd = strEnd;
//
// string retStr;
//
// if(blErase)
// {
// tmpStrSrc=AllTrim(tmpStrSrc);
// }
// if (tmpStrSrc.length()==0)
// {//assign賦值
// retStr=tmpStrSrc.assign(tmpStrEnd);
// }
// else
// { //append會修改之前的字符串
// retStr=tmpStrSrc.append(tmpStrEnd);
// }
// if(!blErase)
// {
// AllTrim(retStr);
// }
// return retStr;
//
// }
string StringUtil::AppendLeft(const string& str,const string& strLeft)
{
string strTemp=str;
strTemp=strLeft+str;
return strTemp;
}
string StringUtil::AppendRight(const string& str,const string& strRight)
{
string strTemp=str;
strTemp=str+strRight;
return strTemp;
}
string StringUtil::AppendChecking(size_t number,const string& str,const string& checkString,string strC,...)
{
string strTemp = str;
string strCheckTemp = checkString;
string strCat = strC;
va_list index = NULL;
int n = 0;
va_start(index,strC);
for(;n<number;n++)
{
strTemp = AppendRight(strTemp,strCat);
if(strstr(strTemp.c_str(),strCheckTemp.c_str()) != NULL)
break;
if(n!=number-1)
strCat = va_arg(index,string);
}
va_end(index);
return strTemp;
}
string StringUtil::GetLine(const string& str,size_t number)
{
string strTemp = str;
string strR;
int iPosiEnd = -1;
int i = 0;
int iPosiStart = 0;
if(number <= 0 )
return strTemp;
for(i = 0 ;i < number; i++)
{
iPosiEnd = strTemp.find("\n",iPosiEnd+1);
strR = strTemp.substr(iPosiStart,iPosiEnd-iPosiStart);//·µ»Ø´Ó0¿ªÊ¼µÄµÚposi¸ö×Ö·û´®
iPosiStart = iPosiEnd+1;
if(iPosiEnd == -1)
break;
}
return strR;
}
string StringUtil::GetLineOfChecking(const string& str,const string& checkString,size_t number)
{
string strTemp = str;
string strR;
int iPosiEnd = -1;
int iPosiStart = 0;
int n = number;
if(number <= 0 )
return strTemp;
for(;;)
{
iPosiEnd = strTemp.find("\n",iPosiEnd+1);
strR = strTemp.substr(iPosiStart,iPosiEnd-iPosiStart);//·µ»Ø´Ó0¿ªÊ¼µÄµÚposi¸ö×Ö·û´®
iPosiStart = iPosiEnd+1;
if(strstr(strR.c_str(),checkString.c_str()))
n--;
if(n==0)
break;
if(iPosiEnd == -1)
break;
}
return strR;
}
string StringUtil::EraseStr(const string& str,const string& checkString)
{
string strTemp = str;
string strR;
int iPosi = 0;
for(;;)
{
iPosi = strTemp.find(checkString,iPosi);
if(iPosi == -1)
break;
strTemp.erase(iPosi,checkString.length());
}
return strTemp;
}
/*
string StringUtil::UnicodeToANSI( const TCHAR* str )
{
char* pTemp;
int iStringLen = 0;
string strTemp;
// wide char to multi char
iStringLen = ::WideCharToMultiByte(CP_ACP,0,str,-1,NULL,0,NULL,NULL );
pTemp = new char[iStringLen + 1];
memset((void*)pTemp,0,sizeof(char)*(iStringLen+1));
::WideCharToMultiByte(CP_ACP,0,str,-1,pTemp,iStringLen,NULL,NULL );
strTemp = pTemp;
delete[] pTemp;
return strTemp;
}*/