StdStringTrimTest.cpp
#includeint main(){ std::string str(" 字符串 String "); std::cout << str << std::endl; std::cout << str.size() << std::endl; str.erase(str.find_first_of(' '), str.find_first_not_of(' ')); str.erase(str.find_last_not_of(' ') + 1); std::cout << str << std::endl; std::cout << str.size() << std::endl; return 0;}
CStyleStringTrimTest.c
#include#include #define STRING_LENGTH 10000int main(){ const char *text = " 字符串 String "; printf("%s\n%u\n", text, strlen(text)); while (*text == ' ') ++text; char str[STRING_LENGTH]; strcpy(str, text); size_t i = strlen(str) - 1; while (str[i] == ' ') str[i--] = '\0'; printf("%s\n%u\n", str, strlen(str)); return 0;}
Compilation.bat
g++ -std=c++11 StdStringTrimTest.cpp -o StdStringTrimTestgcc -std=c11 CStyleStringTrimTest.c -o CStyleStringTrimTest