字符串去空
//去除左边空格
void TrimLeft(char* str) { if (*str == ' ')return; char* t = str; while (*t == ' ')t++; while (*str++ = *t++); } //去除右边空格 void TrimRight(char* str) { while (*str)str++; while(*(--str) == ' ')*str = '\0'; } //一次性去除所有空格 void TrimAll(char* str) { char* t = str; while (*str) { if (*t != ' ') { *str = *t; str++; } t++; } *str = '\0'; }
详细版本:
void Rtrim(char *string) { size_t len = 0; if(string == NULL) return; len = strlen(string); while(len > 0 && string[len-1] == ' ') //位置换一下 string[--len] = 0; return; } //截取字符串首部空格 void Ltrim(char *string) { size_t len = 0; len = strlen(string); char *p_tmp = string; if( (*p_tmp) != ' ') //不是以空格开头 return; //找第一个不为空格的 while((*p_tmp) != '\0') { if( (*p_tmp) == ' ') p_tmp++; else break; } if((*p_tmp) == '\0') //全是空格 { *string = '\0'; return; } char *p_tmp2 = string; while((*p_tmp) != '\0') { (*p_tmp2) = (*p_tmp); p_tmp++; p_tmp2++; } (*p_tmp2) = '\0'; return; }