私は、文字列を持ってみましょう:
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;
}
私は考えていないstd::quoted
(それはあなたの区切り文字、破棄され、次の文字列は、あなたがそれを印刷する前に取り除かれた引用符を持っていた場合に指示する簡単な方法はありませんので、ここでの仕事のための適切なツールである'\"'
デフォルトでは)
私たちは安全にstd::string
のfind
方法に頼ることができると思います。
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;
}
引用符で囲まれた文字を変数内に格納する必要がある場合は、次の行
std::cout << sspace.substr(nextQuote, endQuote-nextQuote+1) << std::endl;
それを得るために簡単に変更できるはずです。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加