C/C++实现删除字符串的首尾空格

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(' ') + );
std::cout << str << std::endl;
std::cout << str.size() << std::endl;
return ;
}

CStyleStringTrimTest.c

#include <stdio.h>
#include <string.h>
#define STRING_LENGTH 10000
int 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) - ;
while (str[i] == ' ')
str[i--] = '\0';
printf("%s\n%u\n", str, strlen(str));
return ;
}

Compilation.bat

g++ -std=c++11 StdStringTrimTest.cpp -o StdStringTrimTest
gcc -std=c11 CStyleStringTrimTest.c -o CStyleStringTrimTest
上一篇:无线局域网络 WIFI/WAPI/WLAN区别浅析


下一篇:Android使用的设计模式1——观察者模式