C++中有不有split函数的功能呀?很急解决方案
C++中有不有split函数的功能呀?很急!
我的一个字符串是这种形式的, "1-56-89-52-41-56 ",我想按 "- "为分隔符取出存入数组,java中有split,不知道C中如何实现呀?
------解决方案--------------------
//////////////////////////////////////////////////////////////////////////////
// Function Name : splitString
// Description : split "source " with "delims " into string vector.
// if str = "123,234|567 " and slipter = ',| ' then
// vec[0] = "123 ", vec[1] = "234 ", vec[2] = "567 "
// this function with more power than above function, coz it support multi-spliter
// and it more effective using the STL method.
// Input Parameters : source -- the string with spliter
// delims -- the spliters
// vec -- output result -- string vector
// Return Value : how many slice had found
//////////////////////////////////////////////////////////////////////////////
size_t splitString(const std::string &source,
const char *delims,
std::vector <std::string> &vec,
bool keepEmpty /*= false*/)
{
// find the index of which not delims
std::string::size_type beg = source.find_first_not_of(delims);
std::string::size_type end;
std::string::size_type nextBeg = beg;
while(beg != std::string::npos)
{
// find the index which is delims, than [beg, end) is the slice we want
end = source.find_first_of(delims, nextBeg+1);
nextBeg = end;
if(end < beg)
{
// we find that case : two delims
if(keepEmpty)
vec.push_back( " ");
}
else if(end != std::string::npos)
{
vec.push_back(source.substr(beg, end-beg));
// update the begin index from the end index
beg = source.find_first_not_of(delims, nextBeg);
}
else
{
vec.push_back(source.substr(beg));
break;
}
}
return vec.size();
}
我的一个字符串是这种形式的, "1-56-89-52-41-56 ",我想按 "- "为分隔符取出存入数组,java中有split,不知道C中如何实现呀?
------解决方案--------------------
//////////////////////////////////////////////////////////////////////////////
// Function Name : splitString
// Description : split "source " with "delims " into string vector.
// if str = "123,234|567 " and slipter = ',| ' then
// vec[0] = "123 ", vec[1] = "234 ", vec[2] = "567 "
// this function with more power than above function, coz it support multi-spliter
// and it more effective using the STL method.
// Input Parameters : source -- the string with spliter
// delims -- the spliters
// vec -- output result -- string vector
// Return Value : how many slice had found
//////////////////////////////////////////////////////////////////////////////
size_t splitString(const std::string &source,
const char *delims,
std::vector <std::string> &vec,
bool keepEmpty /*= false*/)
{
// find the index of which not delims
std::string::size_type beg = source.find_first_not_of(delims);
std::string::size_type end;
std::string::size_type nextBeg = beg;
while(beg != std::string::npos)
{
// find the index which is delims, than [beg, end) is the slice we want
end = source.find_first_of(delims, nextBeg+1);
nextBeg = end;
if(end < beg)
{
// we find that case : two delims
if(keepEmpty)
vec.push_back( " ");
}
else if(end != std::string::npos)
{
vec.push_back(source.substr(beg, end-beg));
// update the begin index from the end index
beg = source.find_first_not_of(delims, nextBeg);
}
else
{
vec.push_back(source.substr(beg));
break;
}
}
return vec.size();
}