以特定的方式分隔句子



我正在用这段代码逐行读取一个文本文件。

    fstream reader;
    string text;
    string readingArray[];
    int length2;
    while (getline(reader, text)) {
                readingArrray[lenght2]=text;
                lenght2++;
            }

在阅读时,我有一句台词"欢迎来到丛林"。我想把这句话分成两部分,比如SAY和"欢迎来到丛林"。

所以我需要;首先,程序应该读取"字符之前的行。之后,程序应该阅读"one_answers"字符之间的部分。我该怎么做?

使用std::string::find 搜索第一次出现的"(不要忘记将其转义为"

然后使用std::string::substring分割字符串。

示例:

int main()
{
    string line = "SAY "Welcome to the jungle"";
    size_t split_pos = line.find('"');
    if (split_pos != string::npos) //No " found
    {
        string firstPart = line.substr(0, split_pos); //Beginning to result
        string secondPart = line.substr(split_pos);   //result to end
        cout << firstPart << endl;
        cout << secondPart << endl;
    }
}

输出:

SAY
"Welcome to the jungle"

最新更新