Recently did a topic, encountered the details of the string to be removed before and after the space. In Java, there seems to be a string function that is trim () that eliminates the space after the string. For C + +, check it, you can refer to a C + + standard library boost, you can easily solve, but to download, set environment variables, so did not go to get. Of course, you can also use regular expressions to match, but it seems to be overqualified. Instead of using the substr () function, and the string has find_last_not_of,find_first_not_of, and so on, it's enough for us to solve the problem.
Copy Code code as follows:
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace Std;
Read each row from the file, and then remove the front and back spaces to connect to a new string.
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;
}