我有任何序列(或句子),我想提取最后 2 个字符串。
例如,
sdfsdfds sdfs dfsd fgsd 3 dsfds
应该产生:3 dsfds
sdfsd (dfgdg)gfdg fg 6 gg
应该产生:6 gg
最佳答案
您可以使用 std::string::find_last_of
查找空格的函数。
int main()
{
std::string test = "sdfsdfds sdfs dfsd fgsd 3 dsfds";
size_t found1 = test.find_last_of( " " );
if ( found1 != string::npos ) {
size_t found2 = test.find_last_of( " ", found1-1 );
if ( found2 != string::npos )
std::cout << test.substr(found2+1, found1-found2-1) << std::endl;
std::cout << test.substr(found1+1) << std::endl;
}
return 0;
}
关于c++ - 从字符串序列中提取最后 2 个单词,以空格分隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3875594/