删除string首尾以及中间的空格

本文介绍如何使用C++标准库中的algorithm和string来删除字符串首尾及中间的空格。示例代码展示了通过erase和remove函数的结合,轻松实现字符串空格的去除,输出结果为纯净的字符序列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

删除string首尾以及中间的空格

对char*字符串进行操作,删除其中的空格,十分的繁琐。若代码为c++代码可以借助标准库算法,可对字符串首尾以及中间的空格进行删除。

#include <iostream>
#include <algorithm>
#include <string>

int main() {
    std::string content = "   11223  3344  ";
    content.erase(remove(content.begin(), content.end(), ' '), content.end());
    std::cout << content << std::endl;
}
// output: 112233344

参考
[1] https://www.codeleading.com/article/92262643209/
[2] https://www.cnblogs.com/alexYuin/p/11546159.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值