在 c 中删除字符串中的空格-ag捕鱼王app官网

当前位置:ag捕鱼王app官网 > > 编程语言 > c >

在 c 中删除字符串中的空格

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

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


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

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

#include #include 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 #include 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 中使用自定义函数从字符串中删除空格

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

#include #include 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。

发布时间: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 引导加载程序进行编程。

发布时间:2024/03/13 浏览次数:151 分类:c

可以使用 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

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图