CSDN博客

img Gengoo

去掉字符串左右两边多余的空格

发表于2008/9/28 14:17:00  2052人阅读

 
 
 
传入字符串string类型, 去掉右边多的空格
  1. /**********************************************************************
  2. *                                                                     *
  3. *   功能: 去掉字符串右边的空格                                        *
  4. *   Data: 2008-07-10                                                  *
  5. *   author:   xgq                                                     *
  6. *                                                                     *
  7. **********************************************************************/
  8. void TrimRightSpace(string &pString)
  9. {
  10.     int     strLength = 0;
  11.     
  12.     if (pString.empty())
  13.     {
  14.         return;
  15.     }
  16.     
  17.     strLength = pString.size();
  18.     
  19.     for (int i=strLength-1; i>=0; --i)
  20.     {
  21.         if ( !isspace(pString[i]))
  22.         {
  23.             pString = pString.substr(0, i+1);
  24.             break;
  25.         }
  26.     }
  27.     return ;
  28. }
传入char *指针型字符串, 去掉右边的空格
  1. /**********************************************************************
  2. *                                                                                                                                         *
  3. *   功能: 去掉字符串左边的空格                                                                                    *
  4. *   Data: 2008-07-10                                                                                                          *
  5. *   author:  xgq                                                                                                                    *
  6. *                                                                                                                                          *
  7. **********************************************************************/
  8. void TrimRightSpace( char* pString )
  9. {
  10.     int      iStringLen;
  11.     int      iCurPos;
  12.     
  13.     if ( pString == NULL )
  14.         return;
  15.     
  16.     iStringLen= strlen( pString );
  17.     
  18.     for ( iCurPos= iStringLen-1; iCurPos>=0; iCurPos-- )
  19.     {
  20.         if ( isspace( pString[ iCurPos ] ) )
  21.         {
  22.             pString[ iCurPos ]= '/0';
  23.             continue;
  24.         }
  25.         else
  26.             break;
  27.     }
  28. }

传入char *类型的字符串指针, 去掉左边的空格

  1. /**********************************************************************
  2. *                                                                                                                                         *
  3. *   功能: 去掉字符串左边的空格                                                                                    *
  4. *   Data: 2008-07-10                                                                                                          *
  5. *   author:  xgq                                                                                                                    *
  6. *                                                                                                                                          *
  7. **********************************************************************/
  8. char* TrimLeftSpace(char* sStr)
  9. {
  10.     if ( !sStr )
  11.         return NULL;
  12.     
  13.     while ( 1 )
  14.     {
  15.         if ( *sStr == 0 )
  16.             return NULL;
  17.         
  18.         if ( !isspace( *sStr ) )
  19.             return sStr;
  20.         
  21.         sStr++;
  22.     }
  23.     
  24.     return NULL;
  25. }

传入string类型的字符串, 去掉左边的空格, 仅在第一个非空格字符前面保留一个空格

  1. /**********************************************************************
  2. *                                                                                                                                         *
  3. *   功能: 去掉字符串左边的空格                                                                                    *
  4. *   Data: 2008-07-10                                                                                                          *
  5. *   author:  xgq                                                                                                                    *
  6. *                                                                                                                                          *
  7. **********************************************************************/
  8. void  TrimLeftSpace(string &pString)
  9. {   
  10.     if (pString.empty())
  11.     {
  12.         return ;
  13.     }
  14.     
  15.     unsigned long i = 0;
  16.     while ( i<pString.size() )
  17.     {
  18.         if ( !isspace( pString.at(i) ) )
  19.         {
  20.             if (i>1)
  21.                 i -= 1;
  22.             pString = pString.substr(i, pString.size());
  23.             break;
  24.         }
  25.         
  26.         ++i;
  27.     }
  28.     
  29.     return ;
  30. }

 

 

 

 

 

0 0

相关博文

我的热门文章

img
取 消
img