static void SplitString1(const std::string& srcStr, std::vector<std::string>& vec, const std::string& separatpor )
{
std::string::size_type posSubStringStart; //子串起始位置
std::string::size_type posSeparator; //分隔符位置
posSeparator = srcStr.find(separatpor);
posSubStringStart = 0;
while( std::string::npos != posSeparator )
{
std::string ss = srcStr.substr(posSubStringStart, posSeparator -posSubStringStart);
if ( ss != "")
vec.push_back(ss);
posSubStringStart = posSeparator + separatpor.size();
posSeparator = srcStr.find(separatpor, posSubStringStart);
}
if ( posSubStringStart != srcStr.length() ) //jiequ zuihou yiduan shuju
{
vec.push_back(srcStr.substr(posSubStringStart));
}
}
static void SplitString2(const std::string& srcStr, std::vector<std::string>& vec, const std::string& separatpor )
{
std::string::size_type pos;
std::string srcStr_2 = srcStr + separatpor; //扩展字符串以方便操作
int size = srcStr_2.size()
for ( int i = 0; i < size; i++ )
{
pos = srcStr_2.find(separatpor, i);
if ( pos < size )
{
std::string s = srcStr_2.substr(i, pos - i);
i = pos + separatpor.size() -1;
vec.push_back(s);
}
}
}
static void SplitString3(const std::string& srcStr, std::vector<std::string>& vec, const std::string& separatpor )
{
std::string a;
std::istringstream ss(srcStr);
while( ss >> a )
{
vec.push_back(a);
}
}
static void SplitString4(const std::string& srcStr, std::vector<std::string>& vec, const std::string& separatpor )
{
std::string line;
std::string stringstream ss(srcStr);
while( getline(ss, line, ' '))//按空格分割
{
if ( !line.empty() )
{
vec.push_back( line );
}
}
}
//去除字符串的首尾空格
static void TrimString(std::string& sSrc)
{
if (sSrc.empty())
return;
sSrc.erase(0, sSrc.find_first_not_of(' '));
sSrc.erase(sSrc.find_last_not_of(' ') + 1);
sSrc.erase(0, sSrc.find_first_not_of('\r'));
sSrc.erase(sSrc.find_last_not_of('\r') + 1);
sSrc.erase(0, sSrc.find_first_not_of('\n'))
}
//移除空串
static void DropEmptrStr(std::vector<std::string>& vec)
{
std::auto it = vec.begin();
while (it != vec.end())
{
if (it->empty())
{
it = vec.erase(it);
}
else
{
++it;
}
}
}
//字符串拷贝,调用者负责内存释放
static char* strDump(const std::string& sSrc)
{
size_t size = sSrc.size() + 1;
char* dst = new char[size];
strcpy_s(dst, size, sSrc.c_str());
return dst;
}
//判断字符串中是否存在要寻找的字符串
static bool HasDstString(const std::string& sSrcStr, const std::string& sDstStr)
{
std::auto pos = sSrcStr.find(sDstStr);
if ( pos != std::string::npos)
{
return true;
}
return false;
}
//判断字符串中是否存在要寻找的字符串,无论大小写
static bool HasDstString(const std::string& sSrcStr, const std::string& sDstStr)
{
std::string tmpsrc = boost::to_lower_copy(sSrcStr);
std::string tmpDst = boost::to_lower_copy(sDstStr);
std::auto pos = tmpsrc .find(tmpDst);
if ( pos != std::string::npos)
{
return true;
}
return false;
}
int main()
{
std::string str;
std::cout << "Please input str: " << std::endl;
getline(std::cin, str);
std::string pattern;
std::cout << "Please input pattern: " << std::endl;
getline(std::cin, pattern);
std::vector<std::string> result = split(str, pattern);
std::cout << "The result: " << std::endl;
for( int i = 0; i < result.size(); i++ )
{
std::cout << result[i] << std::endl;
}
std::cin.get();
return 0;
}