StdStringTrimTest.cpp
#include <iostream> int 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 <stdio.h> #include <string.h> #define STRING_LENGTH 10000 int main() { const char *text = " 字符串 String "; printf("%s %u ", text, strlen(text)); while (*text == ' ') ++text; char str[STRING_LENGTH]; strcpy(str, text); size_t i = strlen(str) - 1; while (str[i] == ' ') str[i--] = '