【发布时间】:2011-09-11 14:34:57
【问题描述】:
这个问题有重复的风险,例如remove double quotes from a string in c++
但我看到的答案都没有解决我的问题
我有一个字符串列表,其中有些是双引号,有些不是,引号总是在开头和结尾
std::vector<std::string> words = boost::assign::list_of("words")( "\"some\"")( "of which")( "\"might\"")("be quoted");
我正在寻找删除引号的最有效方法。这是我的尝试
for(std::vector<std::string>::iterator pos = words.begin(); pos != words.end(); ++pos)
{
boost::algorithm::replace_first(*pos, "\"", "");
boost::algorithm::replace_last(*pos, "\"", "");
cout << *pos << endl;
}
我能做得比这更好吗?我可能有数十万个字符串要处理。它们可能来自文件或数据库。示例中的 std::vector 仅用于说明目的。
【问题讨论】:
-
听起来你最好不要在数据中首先包含引号?
-
@Tomalak 如果我们可以选择我们必须使用的数据:)
-
@Seth:有时,我们可以。有时,我们拥有数据源并试图修复错误的东西。
-
@Potatoswatter 该字符串具有可变长度。有的很短有的很长
-
@Tomalak 如果他有选择的话,我怀疑他会对自己施加奇怪的限制和要求。