1. 程式人生 > >C語言實現去除字串中空格

C語言實現去除字串中空格

去除字串中所有空格
voidVS_StrTrim(char*pStr)
{
char *pTmp = pStr;

while (*pStr != '/0')
{
if (*pStr != ' ')
{
*pTmp++ = *pStr;
}
++pStr;
}
*pTmp = '/0';
}

去除字串右邊空格
void VS_StrRTrim(char *pStr)
{
char *pTmp = pStr+strlen(pStr)-1;

while (*pTmp == ' ')
{
*pTmp = '/0';
pTmp--;
}
}


去除字串左邊空格
void VS_StrLTrim(char *pStr)
{
char *pTmp = pStr;

while (*pTmp == ' ')
{
pTmp++;
}
while(*pTmp != '/0')
{
*pStr = *pTmp;
pStr++;
pTmp++;
}
*pStr = '/0';
}
然後用方法一我轉了一個全是int 型別的DateTime類,程式碼很難看

string DateTime::toString()
{
char temp1[100],temp2[20];
sprintf(temp1, "%d", this->year);
sprintf(temp2, "%d", this->month);
strcat(temp1,temp2);
sprintf(temp2, "%d", this->day);
strcat(temp1,temp2);
sprintf(temp2, "%d", this->hour);
strcat(temp1,temp2);
sprintf(temp2, "%d", this->minute);
strcat(temp1,temp2);
sprintf(temp2, "%d", this->second);
strcat(temp1,temp2);
sprintf(temp2, "%d", this->milliSecond);
strcat(temp1,temp2);
string str(temp1);
return str;
}