c++去掉首尾空格是参考一篇文章的,但是忘记文章出处了,就略过吧。

去掉首尾空格的代码如下:

 void trim(string &s)
{ if( !s.empty() )
{
s.erase(,s.find_first_not_of(" "));
s.erase(s.find_last_not_of(" ") + );
} }

去掉首尾空格

去掉字符串中所有空格的代码如下:

 void trim(string &s)
{
/*
if( !s.empty() )
{
s.erase(0,s.find_first_not_of(" "));
s.erase(s.find_last_not_of(" ") + 1);
}
*/
int index = ;
if( !s.empty())
{
while( (index = s.find(' ',index)) != string::npos)
{
s.erase(index,);
}
} }

去掉所有空格

测试代码如下:

 int main()
{ cout << "-------------------------------------" << endl; string pri = " 7ter 09, jdhfd iere*- ddw jjdjjdj ";
cout << "private string is : \"" << pri << "\"" << endl;
trim(pri);
cout << "after string is : \"" << pri << "\"" << endl; cout << "-------------------------------------" << endl; return ;
}

测试代码

结果如下图:

C++去掉字符串中首尾空格和所有空格的更多相关文章

  1. C++ 去掉字符串的首尾空格和全部空格

    #include <iostream>#include <string>using namespace std; //去掉收尾空格string& ClearHeadTa ...

  2. js去掉字符串中的所有空格

    1.使用js去掉字符串中的所有空格 1.1.定义一个去空格函数方法 function Trim(str,is_global){ var result; result = str.replace(/(^ ...

  3. C# 使用正则表达式去掉字符串中的数字,或者去掉字符串中的非数字

            /// 去掉字符串中的数字           public static string RemoveNumber(string key)           {            ...

  4. 三种java 去掉字符串中的重复字符函数

    三种java 去掉字符串中的重复字符函数 public static void main(string[] args) { system.out.println(removerepeatedchar( ...

  5. 正则匹配去掉字符串中的html标签

    1.得到超链接中的链接地址: string matchString = @"<a[^>]+href=\s*(?:'(?<href>[^']+)'|"&quo ...

  6. C# 使用正则表达式去掉字符串中的数字

    /// <summary>/// 去掉字符串中的数字/// </summary>/// <param name="key"></param ...

  7. 【PHP函数】PHP 去掉字符串中的转义符号

    PHP字符串中的转义符号 string stripslashes ( string $str ) //去掉字符串中的反斜线字符.若是连续二个反斜线,则去掉一个,留下一个.若只有一个反斜线,就直接去掉.

  8. C# .net 使用正则表达式去掉字符串中的数字

    /// <summary>/// 去掉字符串中的数字/// </summary>/// <param name="key"></param ...

  9. 《程序员代码面试指南》第五章 字符串问题 去掉字符串中连续出现k 个0 的子串

    题目 去掉字符串中连续出现k 个0 的子串 java代码 package com.lizhouwei.chapter5; /** * @Description: 去掉字符串中连续出现k 个0 的子串 ...

随机推荐

  1. [转]unity3d 脚本参考-技术文档

    unity3d 脚本参考-技术文档 核心提示:一.脚本概览这是一个关于Unity内部脚本如何工作的简单概览.Unity内部的脚本,是通过附加自定义脚本对象到游戏物体组成的.在脚本对象内部不同志的函数被 ...

  2. javascript的异步编程方法

    一,callback 回调函数 即函数f1和函数f2的关系是f1(f2()); f2作为f1()的回调函数,在f1执行过程中就开始执行f2,先执行线程的主要逻辑,将比较耗时的任务放在后面执行. 回调函 ...

  3. Linux filesystem detection

    16 down vote accepted The reason you can't find it is because, for the most part, it's not in the ke ...

  4. 团队开发——冲刺2.a

    冲刺阶段二(第一天) 1.今天准备做什么? 收集游戏图片:开始.暂停.继续.重新开始.退出……为了界面的后期美工做准备. 2.遇到什么困难? 网上的图片很多,但是比较难找到统一风格的.

  5. 修改FastColoredTextBox控件完成选择

    //判断是否是中文        public bool IsChina(char c)        { bool BoolValue = false;            if (Convert ...

  6. window.location和window.open

    window.location和window.open的区别 window.location = "http://www.baidu.com" 跳转后有后退功能 window.lo ...

  7. LeetCode 【235. Lowest Common Ancestor of a Binary Search Tree】

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

  8. 建造者模式(Builder)

    建造者模式(Builder)将复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示. 建造者模式通常包括下面几个角色: 1. builder:给出一个抽象接口,以规范产品对象的各个组成成分的 ...

  9. python集成开发工具

    1. IDLE http://python.org/idle/ (在 Python 发行版中自带) 2 BlackAdder 3 PythonWorks 4 Wing IDE http://wingw ...

  10. sqlserver 跨服务器访问数据

    需求:两个一模一样的表,分别分布在两个服务器的数据库上,现在要在一个表中,查看这两个表的内容,并让Id排序 1:在本地数据库查询分析器中,运行以下两段语句: --创建链接服务器 exec sp_add ...