zoukankan      html  css  js  c++  java
  • 删除字符串中的空格(空字符)

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

    代码:

    #include<iostream>
    #include<string>
    using namespace std;
    
    /**********************************************************
    *
    *功能:去除字符串中的空字符
    *
    *strSrc:源字符串
    *
    *反回值:NONE
    *
    ***********************************************************/
    void trimAllSpace(string& strSrc)
    {
        string delem = " 	";   //空字符: 空格或者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 = " 	";   //空字符: 空格或者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 = " 	";   //空字符: 空格或者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; 
    }
  • 相关阅读:
    jquery对象中 “冒号” 详解
    vscode自定以vue代码模板
    Vue项目开发环境proxyTable反向代理,生产环境下服务接口地址找不到的解决
    JS函数的节流和防抖
    BFC-块状格式化上下文
    c# 鼠标钩子
    C# 以管理员身份运行WinForm程序
    样式更换
    Revert to this revision 和 Revert changes from this revision 区别
    人工智能几行代码实现换脸
  • 原文地址:https://www.cnblogs.com/lwyeric/p/4989062.html
Copyright © 2011-2022 走看看