2016-11-14 21 views
3

让我有一个字符串:C++ 14名摘录引用的字符串逐字包括引号

string tstring = "Some arbitrarily long string which has \"double quotes\" which has to be printed verbatim"; 

我试图使用stringstreams并引述提取的话

stringstream stream(tstring); 
string tepm; 
while(stream >> std::quoted(temp)) 
    cout << temp << endl; 

但上面跳过引号引用的字符串

Some 
arbitrarily 
. 
. 
double quotes 
. 
. 
verbatim 

我想引用的字符串逐字打印,包含引号

Some 
arbitrarily 
. 
. 
"double quotes" 
. 
. 
verbatim 

我怎么做到这一点使用引用的函数,或者如果它是不可能有一个更好的办法做到这一点(当然从阅读逐个字符的分开,做所有的工作我自己)

编辑:

这里是MCVE的要求

#include <iostream> 
#include <string> 
#include <sstream> 
#include <iomanip> 

using namespace std; 

int main(){ 
    string sspace = "Hi this is \"Real Madrid\""; 
    stringstream stream(sspace); 
    string fpart; 
    while(stream >> quoted(fpart)){ 
     cout << fpart << endl; 
    } 
    return 0; 
} 
+0

'std :: quoted'删除引号。见http://en.cppreference.com/w/cpp/io/manip/quoted。 “quoted”旨在将引用的子字符串视为单个项目,忽略空格。 –

+0

@DavidLively我知道引用是删除引号,因此输出,有没有办法找出它是否被引用并保留引号和字符串? –

+0

嗯..实际上,quoted'的定义似乎表示,当用于像'stream >> std :: quoted(temp)'这样的输入时,应该保留转义的引号。你可以发布一个完整的,可编译的*短*样本吗? –

回答

2

我不认为std::quoted是这里工作的正确工具,因为没有简单的方法可以判断下一个字符串是否有打印前剥离的引号(它会丢弃您的分隔符,默认为'\"'

我认为我们可以安全地回落std::stringfind方法。

  • 包括子程序打印所有的字(空格分隔)不在引号
  • 不断读取直到下一个引号字符服用find优势之内:

全码:

void PrintUnquoted(std::string _in) 
{ 
    std::istringstream ss(_in); 
    std::string temp; 
    while(ss >> temp) 
    { 
     std::cout << temp << '\n'; 
    } 
} 

int main(){ 
    std::string sspace = "Hi this is \"Real Madrid\" etc."; 
    size_t start = 0; 
    size_t nextQuote = 0; 
    while(nextQuote = sspace.find('\"', start), nextQuote != std::string::npos) 
    { 
     size_t endQuote = sspace.find('\"', nextQuote+1); 
     if (endQuote == std::string::npos) 
     { 
      throw std::logic_error("Unmatched quotes"); 
     } 

     PrintUnquoted(sspace.substr(start, nextQuote-start)); 
     std::cout << sspace.substr(nextQuote, endQuote-nextQuote+1) << std::endl; 
     start = endQuote+1; 
    } 
    if (start < sspace.size()) 
    { 
     PrintUnquoted(sspace.substr(start)); 
    } 
    return 0; 
} 

Live Demo

如果您需要将引用字符存储在变量中,则该行应该易于修改以获取该字符。

+0

不错,我想到了这一点,但是我的代码太复杂和混乱了,所以只想尝试其他选项。 –

2

当输入所使用的,std::quoted去除UNESCAP字符串中的ed引号和un-escapes逃脱了引号。所以像这样的字符串:

some "string with" inner quotes 

但对于这项工作,该字符串必须实际报价,在流中逃脱:在阅读时

"some \"string with\" inner quotes" 

成为这一点。如果你这样做:

std::string str = "string \"with some\" quotes"; 
std::stringstream ss (str); 
std::cout << "stream contents: " << ss.str() << std::endl; 

流的内容实际上是:

string "with some" quotes 

逸出你在做的时候,宣布str流中并没有结束,它的存在只为分析器。如果你希望它被写入完全一样的输出流中,你必须把它写这样的而不是:

std::string str = "\"string \\\"with some\\\" quotes\""; 

或更好:

std::string str = "string \"with some\" quotes"; 
ss << std::quoted(str); 

,并留下std::quoted做的工作。

+0

奇怪的是,虽然这似乎有效,但它与cppreference.com示例中的示例直接冲突。 (http://en.cppreference.com/w/cpp/io/manip/quoted) –

+0

你是对的,但我想将引用的字符串存储在一个单独的变量中,我将如何使用你的方法做到这一点?或者至少我需要一种方法来跳过字符串中的空格,除了在引号内 –

+0

@DavidLively它与这个例子有什么冲突? @VikashB你可以用'quoted'将它写入'stringstream',然后用'strea.str()'得到流的内容。 – Ionut

相关问题