Ho 得到一个字符串,直到一个字符的最后一次出现
Ho to get a string till the last occurrence of a character
编辑 1
忘了说字符串要取自cin
我有一个字符串,如“嗨;我的;;名字是安德里亚;我喜欢 C++”。
我想创建一个用该字符串初始化的字符串流,并获取最后一个 ;
.
之前的所有内容
这是我试过的:
string ex("Hi; my;; name is Andrea;; and I like C++"), text, final_text;
stringstream loop_stream(ex);
int findComma = loop_stream.str().find(';');
do {
getline(loop_stream, text, ';');
loop_stream.ignore(1);
final_text += text;
loop_stream << loop_stream.rdbuf();
findComma = loop_stream.str().find(';');
while (findComma == 1) {
loop_stream.ignore(1);
}
findComma = loop_stream.str().find(';');
}
while (findComma != string::npos);
cout << "-" << final_text << "-" << endl;
但它根本不起作用...我希望输出为:
-Hi; my;; name is Andrea-
不使用 stringstream 的更简单的东西怎么样:
string ex("Hi; my;; name is Andrea;; and I like C++");
size_t pos = ex.rfind(';'); // find last semicolon
string final_text = ex.substr(0, pos);
这是另一个使用 find_last_of
的解决方案:
#include <string>
#include <iostream>
int main()
{
std:: string test = "Hi; my;; name is Andrea; and I like C++";
auto pos = test.find_last_of(";");
if ( pos != std::string::npos)
std::cout << test.substr(0, pos);
}
输出:
Hi; my;; name is Andrea
使用函数:
#include <string>
#include <iostream>
std::string parseString(const std::string& s)
{
auto pos = s.find_last_of(";");
if ( pos != std::string::npos)
return s.substr(0, pos);
return s;
}
int main()
{
std::cin >> test; // assume the string will be inputed
std::cout << parseString(test);
}
你陷入了重复循环,检查你的循环情况。我认为 findComma 有一些问题,它的值没有更新。
您可以使用 istreambuf_iterator
从 istream
中的所有内容构造 string
(如 std::cin
)。之后,只需使用其他答案之一来提取您想要的内容:
#include<iostream>
#include<iterator>
#include<sstream>
int main(){
std::istringstream loop_stream("Hi; my;; name is Andrea;; and I like C++");
// read the stream until it's depleted and put it in a string:
std::string all_stream(std::istreambuf_iterator<char>(loop_stream),
std::istreambuf_iterator<char>{});
// use any of the other answers, like this:
std::string final_text;
if(auto pos = all_stream.find_last_of(";"); pos != all_stream.npos) {
final_text = all_stream.substr(0, pos);
}
std::cout << '-' << final_text << "-\n";
}
输出:
-Hi; my;; name is Andrea-
编辑 1
忘了说字符串要取自cin
我有一个字符串,如“嗨;我的;;名字是安德里亚;我喜欢 C++”。
我想创建一个用该字符串初始化的字符串流,并获取最后一个 ;
.
这是我试过的:
string ex("Hi; my;; name is Andrea;; and I like C++"), text, final_text;
stringstream loop_stream(ex);
int findComma = loop_stream.str().find(';');
do {
getline(loop_stream, text, ';');
loop_stream.ignore(1);
final_text += text;
loop_stream << loop_stream.rdbuf();
findComma = loop_stream.str().find(';');
while (findComma == 1) {
loop_stream.ignore(1);
}
findComma = loop_stream.str().find(';');
}
while (findComma != string::npos);
cout << "-" << final_text << "-" << endl;
但它根本不起作用...我希望输出为:
-Hi; my;; name is Andrea-
不使用 stringstream 的更简单的东西怎么样:
string ex("Hi; my;; name is Andrea;; and I like C++");
size_t pos = ex.rfind(';'); // find last semicolon
string final_text = ex.substr(0, pos);
这是另一个使用 find_last_of
的解决方案:
#include <string>
#include <iostream>
int main()
{
std:: string test = "Hi; my;; name is Andrea; and I like C++";
auto pos = test.find_last_of(";");
if ( pos != std::string::npos)
std::cout << test.substr(0, pos);
}
输出:
Hi; my;; name is Andrea
使用函数:
#include <string>
#include <iostream>
std::string parseString(const std::string& s)
{
auto pos = s.find_last_of(";");
if ( pos != std::string::npos)
return s.substr(0, pos);
return s;
}
int main()
{
std::cin >> test; // assume the string will be inputed
std::cout << parseString(test);
}
你陷入了重复循环,检查你的循环情况。我认为 findComma 有一些问题,它的值没有更新。
您可以使用 istreambuf_iterator
从 istream
中的所有内容构造 string
(如 std::cin
)。之后,只需使用其他答案之一来提取您想要的内容:
#include<iostream>
#include<iterator>
#include<sstream>
int main(){
std::istringstream loop_stream("Hi; my;; name is Andrea;; and I like C++");
// read the stream until it's depleted and put it in a string:
std::string all_stream(std::istreambuf_iterator<char>(loop_stream),
std::istreambuf_iterator<char>{});
// use any of the other answers, like this:
std::string final_text;
if(auto pos = all_stream.find_last_of(";"); pos != all_stream.npos) {
final_text = all_stream.substr(0, pos);
}
std::cout << '-' << final_text << "-\n";
}
输出:
-Hi; my;; name is Andrea-