Scene:
1. C + + does not provide a function to remove the front and back spaces of the std::(W) string, such as Trimspace.
2. Many libraries are available, but in order to transplant the code is convenient, it is best to use the standard library to solve the standard library.
The bottom template implements the function of removing spaces. Test.cpp
#include <iostream> #include <stdlib.h> #include <string.h> #include <string> #include < Ctype.h>using namespace STD;//1.VC implementation > 256 crashes. So you have to implement inline int IsSpace (int c) { if (c = = 0x20 | | c = = 0x09 | | c== 0x0D) { return 1; } return 0;} Template<class t>t removepreandlastspace (const t& str) { int length = Str.size (); int i = 0,j = length-1; while (i < length && IsSpace (Str[i])) {I ++;} while (J >= 0 && IsSpace (str[j])) {j--;} cout << i << ":" << j<< endl; if (j<i) return T (); &n bsp; return Str.substr (i,j-i+1);} void Testwstring () { wstring wstr; wstring wres; wstr = L "asdfasd 990 "; wres = RemoVepreandlastspace (WSTR); wcout << "wres:[" << wres << "]" << Endl; wstr = L "ASDFASD 990"; wres = Removepreandlastspace (wstr); Wcout << "res:[" << wres << "]" << endl; wstr = L "ASDFASD 990"; &nbs P wres = Removepreandlastspace (wstr); wcout << "res:[" << wres << "]" < < endl; wstr = L ""; wres = Removepreandlastspace (wstr); wcout << "res:[" << wres << "]" << endl; wstr = L ""; wres = Re Movepreandlastspace (WSTR); wcout << "res:[" << wres << "]" << Endl; wstr = L "; " wres = Removepreandlastspace (wstr); wcout < < "res:[" << wres << "]" << Endl;} void TestString () { string wstr; string wres; wstr = "ASDFASD 990" ; wres = Removepreandlastspace (wstr); cout << "wres:[" << wres << "]" << endl; wstr = "ASDFASD 990"; wres = Removepreandlastspace (WSTR) ; cout << "res:[" << wres << "]" << endl; wstr = "Asdfas D 990 "; wres = Removepreandlastspace (wstr); cout <<" res:["<< wres& nbsp << "]" << endl; wstr = ""; wres = Removepreandlastspace (WSTR); cout << "res:[" << wres << "]" << endl; wstr = "";   ; wres = Removepreandlastspace (wstr); cout << "res:[" << wres << "]" <& Lt endl; wstr = "+"; wres = Removepreandlastspace (wstr); cout << "res:[" << wres << "]" << Endl;} int main (int argc, char const *argv[]) { testwstring (); cout << " ... "<< endl; teststring (); return 0;...................
Output:
0:10WRES:[ASDFASD 990]0:10res:[asdfasd 990]1:11res:[asdfasd 990]0: -1res:[]1: -1res:[]0: -1res:[] ................................. 0:10WRES:[ASDFASD 990]0:10res:[asdfasd 990]1:11res:[asdfasd 990]0: -1res:[]1: -1res:[]0: -1res:[]
[C + + standard library]_[primary]_[use template to delete strings before and after spaces ((w) string space)]