实现代码如下:
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
//从文件中读取每一行,然后消除前后空格,使其连成一个新的字符串。
int main()
{
string newstring = "";
vector<string> str;
ifstream fin("a.txt");
string line;
while (getline(fin, line))
str.push_back(line);
for (unsigned i = 0; i < str.size(); i++)
{
newstring += str[i].substr(str[i].find_first_not_of(" "),str[i].find_last_not_of(" ")-str[i].find_first_not_of(" ")+1);
}
cout<<newstring<<endl;
return 0;
}
以上就是【C++中用substr()函数消除前后空格的解决方法详解】的全部内容了,欢迎留言评论进行交流!