C + + Remove the first and last space is a reference to an article, but forget the article source, just skip it.
The code to remove the trailing spaces is as follows:
void Trim (String &s) { if (!s.empty ()) { s.erase (0,s.find_first_not_of ("")); S.erase (S.find_last_not_of ("") + 1);} } Remove the leading and trailing spaces
The code for removing all the spaces in the string is as follows:
void Trim (string &s) {/ * if (!s.empty ()) { s.erase (0,s.find_first_not_of ("")); S.erase (S.find_last_not_of ("") + 1); } */ int index = 0; if (!s.empty ()) { while (index = S.find (', index))! = String::npos) { s.erase (index,1); } }} Remove all spaces
The test code is as follows:
int main () { cout << "-------------------------------------" << Endl; String pri = " 7ter , jdhfd iere*-ddw jjdjjdj "; cout << "private string is: \" "<< pri <<" \ "" << Endl; Trim (PRI); cout << "After string is: \" "<< pri <<" \ "" << Endl; cout << "-------------------------------------" << Endl; return 0;} Test code
Results such as:
C + + Removes the leading and trailing spaces and all spaces in a string