から2重引用符を抽出好き。C++、私は私がそれらを分類することができますように、2つの文字列に引用符を抽出しているよ「ジョン」「こんにちは」</p> </blockquote> <p>私は、文字列</p> <blockquote> <p>を持つ文字列
User: John
Text: Hello there
これを行うにはどうすればよいのでしょうか?これを簡単なプロセスにするために適用できる文字列関数はありますか?
から2重引用符を抽出好き。C++、私は私がそれらを分類することができますように、2つの文字列に引用符を抽出しているよ「ジョン」「こんにちは」</p> </blockquote> <p>私は、文字列</p> <blockquote> <p>を持つ文字列
User: John
Text: Hello there
これを行うにはどうすればよいのでしょうか?これを簡単なプロセスにするために適用できる文字列関数はありますか?
使用std::quoted
:http://en.cppreference.com/w/cpp/io/manip/quoted
#include <iomanip>
#include <sstream>
#include <iostream>
int main() {
std::string user, text;
std::istringstream iss("\"John\" \"Hello there\"");
if (iss >> std::quoted(user) >> std::quoted(text)) {
std::cout << "User: " << user << "\n";
std::cout << "Text: " << text << "\n";
}
}
注意、それはまた、引用符をエスケープサポート:入力がMe "This is a \"quoted\" word"
であれば、それは(もLive)
User: Me
Text: This is a "quoted" word
ライブデモを追加しました – sehe
これは、使用可能な解決策であるstringstream
:
std::string name = "\"Jhon\" \"Hello There\"";
std::stringstream ss{name};
std::string token;
getline(ss, token, '\"');
while (!ss.eof())
{
getline(ss, token, '\"');
ss.ignore(256, '\"');
std::cout << token << std::endl;
}
出力:[?文字列を分割するための最もエレガントな方法]
Jhon
Hello There
の可能性のある重複(https://stackoverflow.com/question/236129/most-elegant-way-to-split-a-string) – user463035818
uhm、 'easy'を定義する...質問は多少曖昧です... gaziliionの可能な要件... –