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

//去掉收尾空格
string& ClearHeadTailSpace(string &str)   
{  
if (str.empty())   
{  
return str;  
}

str.erase(0,str.find_first_not_of(" "));  
str.erase(str.find_last_not_of(" ") + 1);  
return str;  
}

//去掉字符串中的全部空格
string& ClearAllSpace(string &str)
{
    int index = 0;
    if( !str.empty())
    {
        while( (index = str.find(' ',index)) != string::npos)
        {
            str.erase(index,1);
        }
    }

return str;
}

int main()
{
string str = "  123   456  789   ";

cout << ClearHeadTailSpace(str) << endl;
cout << ClearAllSpace(str) << endl;

system("pause");
return 0;
}
————————————————

原文链接:https://blog.csdn.net/yao_hou/article/details/78840723

C++ 去掉字符串的首尾空格和全部空格的更多相关文章

  1. C++去掉字符串中首尾空格和所有空格

    c++去掉首尾空格是参考一篇文章的,但是忘记文章出处了,就略过吧. 去掉首尾空格的代码如下: void trim(string &s) { if( !s.empty() ) { s.erase ...

  2. 用javascript去掉字符串空格的办法

    今天遇到了以关于JavaScript 中怎么去掉 字符串中前后两段的空格 ,我只好向就得js中也后Trim() 函数,后来试试了不 行,就网上找了下解决方法,其中用到了正则表达式 ,整理了下: < ...

  3. C++去掉字符串首尾的 空格 换行 回车

    /* *去掉字符串首尾的 \x20 \r \n 字符 */ void TrimSpace(char* str) { ; char *end = str; char *p = str; while(*p ...

  4. c#中去掉字符串空格方法

    (1)Trim方法 string   tt=" aaa "; tt=tt.Trim()       去字符串首尾空格的函数 tt=tt.TrimEnd() 去掉字符串尾空格 tt= ...

  5. js去掉html标签和去掉字符串文本的所有的空格

    去掉html标签的js <script> function delHtmlTag(str){  return str.replace(/<[^>]+>/g,"& ...

  6. C++ 去掉字符串首尾的 \x20 \r \n 字符

    转载:http://www.sharejs.com/codes/cpp/5780 /* 去掉字符串首尾的 \x20 \r \n 字符 by sincoder */ void clean_string( ...

  7. 【trim()】去掉字符串开头和结尾的空格,防止不必要的空格导致的错误。

    去掉字符串开头和结尾的空格,防止不必要的空格导致的错误. public static void main(String arg[]){ String a=" abc"; Strin ...

  8. as3中去掉字符串两边的空格,换行符

     as3 去掉字符串两边的空格,换行符,方法一  ActionScript Code  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20   pub ...

  9. String的trim()用于去掉字符串前后的空格

    String的trim()可以去掉字符串的前导和后继字符串,即去掉字符串前面和后面的空格. eg:String userName = " good man "; System.ou ...

随机推荐

  1. 20161209pod search 'fmdb'提示[!] Unable to find a pod with name, author, summary, or description matching `fmdb`

    从SVN上更新工程之后运行工程提示错误: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update y ...

  2. 在linux的用户空间操作gpio

    1. 使能linux内核选项CONFIG_GPIO_SYSFS CONFIG_GPIO_SYSFS=y 2. 测试方法 2.1 关注/sys/class/gpio下的文件 --export/unexp ...

  3. RabbitMQ 入门教程(PHP版) 第六部分:远程调用(RPC)

    在云计算环境中,很多时候需要用它其他机器的计算资源,把一部分计算任务分配到其他节点来完成.RabbitMQ 如何使用 RPC 呢?下面将会通过其它节点完成斐波纳契示例. 流程图  当客户端启动时,它 ...

  4. 用KNN实现iris的4分类问题&测试精度

    import matplotlib.pyplot as plt from scipy import sparse import numpy as np import matplotlib as mt ...

  5. attribute__关键字举例之visibility

    /** @file visibilityT.c * @note * @brief * @author * @date 2019-6-20 * @note v1.0.0 Created * @histo ...

  6. 线性回归:鸢尾花数据iris

    # encoding: utf-8 from sklearn.linear_model import LogisticRegression import numpy as np from sklear ...

  7. PhpStorm (强大的PHP开发环境)10.0.2 附注

    最新版PhpStorm 10正式版改进了PHP 7支持,改进代码完成功能. PhpStorm 是最好的PHP开发工具,使用它进行PHP开发将会让你感觉到编程的乐趣. 快乐无极终于从oschina看到了 ...

  8. SET IDENTITY_INSERT的用法,具体去体验一下

    如果将值插入到表的标识列中,需要启用 SET IDENTITY_INSERT. 举例如下: 创建表Orders.Products,Orders表与Products表分别有标识列OrderID与Prod ...

  9. Django:序列化的几种方法

    前言 关于序列化操作,就是将一个可迭代的数据结构,通过便利的方式进行我们所需要的操作. 今天历来归纳一下,Django中的几种不同得分方法,已经Django-restframework提供的方法 创建 ...

  10. 解释张量及TF的一些API

    张量的定义 张量(Tensor)理论是数学的一个分支学科,在力学中有重要应用.张量这一术语起源于力学,它最初是用来表示弹性介质中各点应力状态的,后来张量理论发展成为力学和物理学的一个有力的数学工具.张 ...