105 lines
2.5 KiB
C++
105 lines
2.5 KiB
C++
|
|
#include "VrStringUtils.h"
|
|||
|
|
#include <sstream>
|
|||
|
|
#include <algorithm>
|
|||
|
|
#include <iomanip>
|
|||
|
|
|
|||
|
|
/// \brief
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
|
/// \param nNumber[in] <20><><EFBFBD><EFBFBD>
|
|||
|
|
/// \return <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
|
std::string CVrStringUtils::Int2Str(int nNum)
|
|||
|
|
{
|
|||
|
|
std::stringstream ss;
|
|||
|
|
ss << nNum;
|
|||
|
|
return ss.str();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
std::string CVrStringUtils::Int2HexStr(int nNum)
|
|||
|
|
{
|
|||
|
|
std::stringstream ss;
|
|||
|
|
ss << std::hex << std::setw(2) << std::setfill('0') << std::uppercase << nNum; // ʹ<><CAB9> std::hex <20><>־<EFBFBD><D6BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʮ<EFBFBD><CAAE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
return ss.str(); // ʹ<><CAB9> str() <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD>е<EFBFBD><D0B5>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ʾ
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
std::string CVrStringUtils::Int2Str(unsigned int nNum)
|
|||
|
|
{
|
|||
|
|
std::stringstream ss;
|
|||
|
|
ss << nNum;
|
|||
|
|
return ss.str();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
std::string CVrStringUtils::Int2Str(unsigned long long nNum)
|
|||
|
|
{
|
|||
|
|
std::stringstream ss;
|
|||
|
|
ss << nNum;
|
|||
|
|
return ss.str();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
std::string CVrStringUtils::Int2str(long num)
|
|||
|
|
{
|
|||
|
|
std::stringstream ss;
|
|||
|
|
ss << num;
|
|||
|
|
return ss.str();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
std::vector<std::string> CVrStringUtils::SplitString(std::string srcStr, std::string delimStr, bool repeatedCharIgnored)
|
|||
|
|
{
|
|||
|
|
std::vector<std::string> resultStringVector;
|
|||
|
|
std::replace_if(srcStr.begin(), srcStr.end(), [&](const char& c) {if (delimStr.find(c) != std::string::npos) { return true; } else { return false; }}/*pred*/, delimStr.at(0));//<2F><><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>зָ<D0B7><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>滻<EFBFBD><E6BBBB>Ϊһ<CEAA><D2BB><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>ĵ<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>
|
|||
|
|
size_t pos = srcStr.find(delimStr.at(0));
|
|||
|
|
std::string addedString = "";
|
|||
|
|
while (pos != std::string::npos) {
|
|||
|
|
addedString = srcStr.substr(0, pos);
|
|||
|
|
if (!addedString.empty() || !repeatedCharIgnored) {
|
|||
|
|
resultStringVector.push_back(addedString);
|
|||
|
|
}
|
|||
|
|
srcStr.erase(srcStr.begin(), srcStr.begin() + pos + 1);
|
|||
|
|
pos = srcStr.find(delimStr.at(0));
|
|||
|
|
}
|
|||
|
|
addedString = srcStr;
|
|||
|
|
if (!addedString.empty() || !repeatedCharIgnored) {
|
|||
|
|
resultStringVector.push_back(addedString);
|
|||
|
|
}
|
|||
|
|
return resultStringVector;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
int CVrStringUtils::StringUpper(char *sData, int nLen)
|
|||
|
|
{
|
|||
|
|
if (sData != nullptr && nLen != 0)
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < nLen; i++)
|
|||
|
|
{
|
|||
|
|
sData[i] = toupper(sData[i]);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
int CVrStringUtils::StringLower(char *sData, int nLen)
|
|||
|
|
{
|
|||
|
|
if (sData != nullptr && nLen != 0)
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < nLen; i++)
|
|||
|
|
{
|
|||
|
|
sData[i] = tolower(sData[i]);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <20>滻<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
|
void CVrStringUtils::ReplaceString(std::string& src, const std::string& repSrc, const std::string& repDest)
|
|||
|
|
{
|
|||
|
|
size_t pos = 0;
|
|||
|
|
// <20>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼλ<CABC><CEBB>
|
|||
|
|
while ((pos = src.find(repSrc, pos)) != std::string::npos)
|
|||
|
|
{
|
|||
|
|
// <20>滻<EFBFBD>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
|||
|
|
src.replace(pos, repSrc.length(), repDest);
|
|||
|
|
// <20>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD>滻<EFBFBD><E6BBBB><EFBFBD><EFBFBD>λ<EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>滻<EFBFBD>Ѿ<EFBFBD><D1BE>滻<EFBFBD>IJ<EFBFBD><C4B2><EFBFBD>
|
|||
|
|
pos += repDest.length();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|