迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > C++ >

在 C++ 中删除字符串中的空格

作者:迹忆客 最近更新:2023/04/08 浏览次数:

本文将介绍有关如何在 C++ 中从字符串中删除空格的多种方法。


使用 erase-remove 习惯用法从 C++ 中的字符串中删除空格

C++ 中用于范围操作的最有用的方法之一是 erase-remove 习惯用法,它包含两个函数-std::erase(大多数 STL 容器的内置函数)和 std::remove(STL 算法库的一部分)。请注意,它们都链接在一起以对给定的对象执行删除操作。std::remove 函数需要两个迭代器来指定范围,第三个参数表示要删除的元素的值。在这种情况下,我们直接指定一个空格字符,但是可以指定任何字符以删除字符串中所有出现的字符。

#include <iostream>
#include <string>

using std::cout; using std::cin;
using std::endl; using std::string;

int main(){
    string str = "  Arbitrary   str ing with lots of spaces to be removed   .";

    cout << str << endl;

    str.erase(std::remove(str.begin(), str.end(), ' '), str.end());

    cout << str << endl;

    return EXIT_SUCCESS;
}

输出:

Arbitrary   str ing with lots of spaces to be removed   .
Arbitrarystringwithlotsofspacestoberemoved.

另一方面,用户也可以将一元谓词作为第三个参数传递给 std::remove 算法。谓词应为每个元素求出布尔值,并且当结果为 true 时,将从范围中删除相应的值。因此,我们可以利用预定义的 isspace 函数来检查多个空格字符,例如空格-" ",换行符-\n,水平制表符-\t 以及其他几个字符。

#include <iostream>
#include <string>

using std::cout; using std::cin;
using std::endl; using std::string;

int main(){
    string str = "  Arbitrary   str ing with lots of spaces to be removed   .";

    cout << str << endl;

    str.erase(std::remove_if(str.begin(), str.end(), isspace), str.end());

    cout << str << endl;

    return EXIT_SUCCESS;
}

输出:

Arbitrary   str ing with lots of spaces to be removed   .
Arbitrarystringwithlotsofspacestoberemoved.

在 C++ 中使用自定义函数从字符串中删除空格

请注意,所有以前的解决方案都修改了原始的字符串对象,但是有时,可能需要创建一个删除所有空格的新字符串。我们可以使用相同的 erase-remove 惯用语来实现自定义函数,该惯用语接受字符串引用并返回已解析的值,以将其存储在单独的字符串对象中。也可以修改此方法以支持另一个函数参数,该参数将指定需要删除的字符。

#include <iostream>
#include <string>

using std::cout; using std::cin;
using std::endl; using std::string;

string removeSpaces(const string& s) {
    string tmp(s);
    tmp.erase(std::remove(tmp.begin(), tmp.end(), ' '), tmp.end());
    return tmp;
}

int main(){
    string str = "  Arbitrary   str ing with lots of spaces to be removed   .";

    cout << str << endl;

    string newstr = removeSpaces(str);

    cout << newstr << endl;

    return EXIT_SUCCESS;
}

输出:

Arbitrary   str ing with lots of spaces to be removed   .
Arbitrarystringwithlotsofspacestoberemoved.

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Arduino 中停止循环

发布时间:2024/03/13 浏览次数:444 分类:C++

可以使用 exit(0),无限循环和 Sleep_n0m1 库在 Arduino 中停止循环。

Arduino 复位

发布时间:2024/03/13 浏览次数:315 分类:C++

可以通过使用复位按钮,Softwarereset 库和 Adafruit SleepyDog 库来复位 Arduino。

Arduino 的字符转换为整型

发布时间:2024/03/13 浏览次数:181 分类:C++

可以使用简单的方法 toInt()函数和 Serial.parseInt()函数将 char 转换为 int。

Arduino 串口打印多个变量

发布时间:2024/03/13 浏览次数:381 分类:C++

可以使用 Serial.print()和 Serial.println()函数在串口监视器上显示变量值。

Arduino if 语句

发布时间:2024/03/13 浏览次数:123 分类:C++

可以使用 if 语句检查 Arduino 中的不同条件。

Arduino ICSP

发布时间:2024/03/13 浏览次数:214 分类:C++

ICSP 引脚用于两个 Arduino 之间的通信以及对 Arduino 引导加载程序进行编程。

使用 C++ 编程 Arduino

发布时间:2024/03/13 浏览次数:127 分类:C++

本教程将讨论使用 Arduino IDE 在 C++ 中对 Arduino 进行编程。

Arduino 中的子程序

发布时间:2024/03/13 浏览次数:168 分类:C++

可以通过在 Arduino 中声明函数来处理子程序。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便