如何通过分隔符标记字符串
How to tokenize string by delimiters?
我需要用分隔符标记字符串。
例如:
对于"One, Two Three,,, Four"
,我需要得到{"One", "Two", "Three", "Four"}
。
我正试图利用这种方法https://stackoverflow.com/a/55680/1034253
std::vector<std::string> strToArray(const std::string &str,
const std::string &delimiters = " ,")
{
boost::char_separator<char> sep(delimiters.c_str());
boost::tokenizer<boost::char_separator<char>> tokens(str.c_str(), sep);
std::vector<std::string> result;
for (const auto &token: tokens) {
result.push_back(token);
}
return result;
}
但我得到了错误:
boost-1_57\boost/tokenizer.hpp(62):错误C2228:'.begin'的左侧必须具有类/结构/并集类型为"const char*const"
更改此项:
boost::tokenizer<boost::char_separator<char>> tokens(str.c_str(), sep);
对此:
boost::tokenizer<boost::char_separator<char>> tokens(str, sep);
链接:http://www.boost.org/doc/libs/1_57_0/libs/tokenizer/tokenizer.htm
容器类型需要一个begin()
函数,而返回的const char*(这就是c_str()
)不满足此要求。
Boost的标记化器对于您描述的任务来说可能有些过头了。
boost::split
就是为这个任务编写的。
std::vector<std::string> strToArray(const std::string &str,
const std::string &delimiters = " ,")
{
using namespace boost;
std::vector<std::string> result;
split( result, str, is_any_of(delimiters), token_compress_on );
return result;
}
该可选的token_compress_on
表示您的,,,
输入不应在这些逗号之间暗示空字符串标记。
捷径。
string tmp = "One, Two, Tree, Four";
int pos = 0;
while (pos = tmp.find(", ") and pos > 0){
string s = tmp.substr(0, pos);
tmp = tmp.substr(pos+2);
cout << s;
}
我看到了很多boost
的答案,所以我想我应该提供一个非boost
的答案:
template <typename OutputIter>
void Str2Arr( const std::string &str, const std::string &delim, int start, bool ignoreEmpty, OutputIter iter )
{
int pos = str.find_first_of( delim, start );
if (pos != std::string::npos) {
std::string nStr = str.substr( start, pos - start );
trim( nStr );
if (!nStr.empty() || !ignoreEmpty)
*iter++ = nStr;
Str2Arr( str, delim, pos + 1, ignoreEmpty, iter );
}
else
{
std::string nStr = str.substr( start, str.length() - start );
trim( nStr );
if (!nStr.empty() || !ignoreEmpty)
*iter++ = nStr;
}
}
std::vector<std::string> Str2Arr( const std::string &str, const std::string &delim )
{
std::vector<std::string> result;
Str2Arr( str, delim, 0, true, std::back_inserter( result ) );
return std::move( result );
}
trim
可以是任何修剪函数,我使用了这个SO答案。它利用了CCD_ 11和递归。你可以很容易地在一个循环中完成,但这听起来更有趣:)
相关文章:
- 使用 char 分隔符解析C++中的字符串,但将可重复的字符保留为每个解析的子字符串 (C++ STL) 中的分隔符
- 字符串开头的分隔符
- 尝试将 c 字符串数组与分隔符连接起来
- 根据新的行分隔符从字符串中删除子字符串
- 同一分隔符之间的多个子字符串
- 字符串流分隔符
- 使用正则表达式c++从单词和分隔符之间的字符串中提取所有子字符串
- C++获取两个分隔符之间的字符串并替换它
- 是否存在像C++中那样带有分隔符的C#原始字符串
- 如何从字符串开头到第二个分隔符中提取子字符串?
- 如何使用两个不同的分隔符拆分字符串
- 将子字符串提取到最后 n 个分隔符C++
- 基于 C++ 中的多个字符串分隔符拆分字符串
- 读取 iostream,直到找到字符串分隔符
- 带有字符串分隔符或两个字符的fstream
- 读取直到boost::asio::streambuf中的字符串分隔符
- 使用两个字符串分隔符选择文本文件的一部分
- C++std::ifstream读取字符串分隔符
- 使用多个字符串分隔符拆分字符串
- 使用字符串分隔符(标准C++)解析(拆分)C++中的字符串