截取字符串
string strTributeInfo = "1#2#3#4#5#6#7";
vector<string> vecTribute;
StringUtil::Parse2Tokens(strTributeInfo,"#",vecTribute);
int StringUtil::Parse2Tokens(string strPackage, const string & strSplit, vector<string> & vecTokens)
{
int nFound = strPackage.find(strSplit); //find 函数 返回strSplit 在strPackage中的下标位置
while (nFound != -1)
{
//substr 方法 返回一个从指定位置开始,并具有指定长度的子字符串。
string strToken = strPackage.substr(0, nFound);
vecTokens.push_back(strToken);
strPackage = strPackage.substr(nFound + 1, strPackage.length());
nFound = strPackage.find(strSplit);
}
vecTokens.push_back(strPackage);
return vecTokens.size();
}
将字符串转化成int类型
bool StringUtil::ParseString2Int(const string & strString, int & nRet)
{
bool bRet = CheckIsNumber(strString);
if (!bRet)
{
nRet = 0;
return false;
}
sscanf(strString.c_str(), "%d", &nRet);
//sscanf() - 从一个字符串中读进与指定格式相符的数据.
return true;
}
bool StringUtil::CheckIsNumber(const string & strNumber)
{
int nLoopCountStringLen = strNumber.length();
int nLoopCount = -1;
for (int j = 0; j < nLoopCountStringLen; j++)
{
char pCh = strNumber.at(j);
if (j == 0)
{
if (pCh == '+' || pCh == '-') continue;
}
if (pCh >= '0' && pCh <= '9') continue;
if (pCh == '.' && j < nLoopCountStringLen - 1) continue;
// find non number
return false;
}
return true;
}
string StringUtil::IntToStr(const int &nValue)
{
string strRent;
stringstream ss;
ss << nValue;
ss >> strRent;
return strRent;
}