首页 > 其他 > 详细

删除字符串中的空格(空字符)

时间:2015-11-23 18:45:55      阅读:272      评论:0      收藏:0      [点我收藏+]

C++中的字符串过滤空格(空字符),可以使用string自带的方法实现。

代码:

#include<iostream>
#include<string>
using namespace std;

/**********************************************************
*
*功能:去除字符串中的空字符
*
*strSrc:源字符串
*
*反回值:NONE
*
***********************************************************/
void trimAllSpace(string& strSrc)
{
    string delem = " \t";   //空字符: 空格或者tab键
    string::size_type pos = strSrc.find_first_of(delem, 0);
    while (pos != string::npos)
    {
        strSrc.erase(pos, 1);
        pos = strSrc.find_first_of(delem, 0);
    }
    return;
}

/**********************************************************
*
*功能:去除字符串中前导空字符
*
*strSrc:源字符串
*
*反回值:NONE
*
***********************************************************/
void trimLeftSpace(string& strSrc)
{
    string delem = " \t";   //空字符: 空格或者tab键
    string::size_type pos = strSrc.find_first_not_of(delem);
    if (pos != string::npos)
    {
        strSrc.erase(0,pos);
    }
    return;  
}

/**********************************************************
*
*功能:去除字符串中尾部空字符
*
*strSrc:源字符串
*
*反回值:NONE
*
***********************************************************/
void trimRightSpace(string& strSrc)
{
    string delem = " \t";   //空字符: 空格或者tab键
    string::size_type pos = strSrc.find_last_not_of(delem);
    if (pos != string::npos)
    {
        strSrc = strSrc.substr(0,pos+1);
    }
    return; 
}

/**********************************************************
*
*功能:去除字符串中两端空字符
*
*strSrc:源字符串
*
*反回值:NONE
*
***********************************************************/
void trimLeftRightSpace(string& strSrc)
{
    trimLeftSpace(strSrc);
    trimRightSpace(strSrc);
    return; 
}

 

删除字符串中的空格(空字符)

原文:http://www.cnblogs.com/lwyeric/p/4989062.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!