C语言实现去除字符串中空格

C语言实现去除字符串中空格

 (2012-04-10 13:25:00)
   
  1.   去除字符串中所有空格 
  2. voidVS_StrTrim(char*pStr)  
  3. {  
  4.     char *pTmp = pStr;  
  5.       
  6.     while (*pStr != '/0')   
  7.     {  
  8.         if (*pStr != ' ')  
  9.         {  
  10.             *pTmp++ = *pStr;  
  11.         }  
  12.         ++pStr;  
  13.     }  
  14.     *pTmp = '/0';  
  15. }  
  16.   
  17.   去除字符串右边空格
  18. void VS_StrRTrim(char *pStr)  
  19. {  
  20.     char *pTmp = pStr+strlen(pStr)-1;  
  21.       
  22.     while (*pTmp == ' ')   
  23.     {  
  24.         *pTmp = '/0';  
  25.         pTmp--;  
  26.     }  
  27. }  
  28.   
  29.  
  30. 去除字符串左边空格
  31. void VS_StrLTrim(char *pStr)  
  32. {  
  33.     char *pTmp = pStr;  
  34.       
  35.     while (*pTmp == ' ')   
  36.     {  
  37.         pTmp++;  
  38.     }  
  39.     while(*pTmp != '/0')  
  40.     {  
  41.         *pStr = *pTmp;  
  42.         pStr++;  
  43.         pTmp++;  
  44.     }  
  45.     *pStr = '/0';  
  46. }
    然后用方法一我转了一个全是int 类型的DateTime类,代码很难看

  1. string DateTime::toString()
  2. {
  3.     char temp1[100],temp2[20];
  4.     sprintf(temp1, "%d", this->year);
  5.     sprintf(temp2, "%d", this->month);
  6.     strcat(temp1,temp2);
  7.     sprintf(temp2, "%d", this->day);
  8.     strcat(temp1,temp2);
  9.     sprintf(temp2, "%d", this->hour);
  10.     strcat(temp1,temp2);
  11.     sprintf(temp2, "%d", this->minute);
  12.     strcat(temp1,temp2);
  13.     sprintf(temp2, "%d", this->second);
  14.     strcat(temp1,temp2);
  15.     sprintf(temp2, "%d", this->milliSecond);
  16.     strcat(temp1,temp2);
  17.     string str(temp1);
  18.     return str;
  19. }
posted @ 2016-03-16 15:18  强者最帅  阅读(1436)  评论(0)    收藏  举报