C++ 字符串处理-去除字符串前后的空字符

2024-05-25 21:36

本文主要是介绍C++ 字符串处理-去除字符串前后的空字符,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  • 1. 关键词
  • 2. strutil.h
  • 3. strutil.cpp
  • 4. 测试代码
  • 5. 运行结果
  • 6. 源码地址

1. 关键词

C++ 字符串处理 去除字符串前后的空字符 跨平台

2. strutil.h

#include <string>
namespace cutl
{/*** @brief Remove leading whitespaces from a string.** @param str the string to be stripped.* @return std::string the stripped string.*/std::string lstrip(const std::string &str);/*** @brief Remove trailing whitespaces from a string.** @param str the string to be stripped.* @return std::string the stripped string.*/std::string rstrip(const std::string &str);/*** @brief Remove leading and trailing whitespaces from a string.** @param str the string to be stripped.* @return std::string the stripped string.*/std::string strip(const std::string &str);
} // namespace cutl

3. strutil.cpp

#include <cctype>
#include <algorithm>
#include "strutil.h"namespace cutl
{std::string lstrip(const std::string &str){if (str.empty()){return "";}size_t index = 0;for (size_t i = 0; i < str.length(); i++){if (!std::isspace(str[i])){index = i;break;}}return str.substr(index, str.length() - index);}std::string rstrip(const std::string &str){if (str.empty()){return "";}size_t index = str.length() - 1;for (size_t i = str.length() - 1; i >= 0; i--){if (!std::isspace(str[i])){index = i;break;}}return str.substr(0, index + 1);}std::string strip(const std::string &str){if (str.empty()){return "";}size_t index1 = 0;for (size_t i = 0; i < str.length(); i++){if (!std::isspace(str[i])){index1 = i;break;}}size_t index2 = str.length() - 1;for (size_t i = str.length() - 1; i >= 0; i--){if (!std::isspace(str[i])){index2 = i;break;}}auto len = index2 - index1 + 1;return str.substr(index1, len);}
} // namespace cutl

4. 测试代码

#include "common.hpp"
#include "strutil.h"void TestStrip()
{PrintSubTitle("TestStrip");std::string text = "  \tThis is a test string. \n ";// std::string text = "  \t中国 \n ";std::cout << "text: " << text << std::endl;std::cout << "trim left text: " << cutl::lstrip(text) << std::endl;std::cout << "trim right text: " << cutl::rstrip(text) << std::endl;std::cout << "trim text: " << cutl::strip(text) << std::endl;
}

5. 运行结果

---------------------------------------------TestStrip----------------------------------------------
text:   	This is a test string. trim left text: This is a test string. trim right text:   	This is a test string.
trim text: This is a test string.

6. 源码地址

更多详细代码,请查看本人写的C++ 通用工具库: common_util, 本项目已开源,代码简洁,且有详细的文档和Demo。

这篇关于C++ 字符串处理-去除字符串前后的空字符的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1002681

相关文章

Java中的StringBuilder之如何高效构建字符串

《Java中的StringBuilder之如何高效构建字符串》本文将深入浅出地介绍StringBuilder的使用方法、性能优势以及相关字符串处理技术,结合代码示例帮助读者更好地理解和应用,希望对大家... 目录关键点什么是 StringBuilder?为什么需要 StringBuilder?如何使用 St

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

一文详解Java异常处理你都了解哪些知识

《一文详解Java异常处理你都了解哪些知识》:本文主要介绍Java异常处理的相关资料,包括异常的分类、捕获和处理异常的语法、常见的异常类型以及自定义异常的实现,文中通过代码介绍的非常详细,需要的朋... 目录前言一、什么是异常二、异常的分类2.1 受检异常2.2 非受检异常三、异常处理的语法3.1 try-

Java中字符串转时间与时间转字符串的操作详解

《Java中字符串转时间与时间转字符串的操作详解》Java的java.time包提供了强大的日期和时间处理功能,通过DateTimeFormatter可以轻松地在日期时间对象和字符串之间进行转换,下面... 目录一、字符串转时间(一)使用预定义格式(二)自定义格式二、时间转字符串(一)使用预定义格式(二)自

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

Java Response返回值的最佳处理方案

《JavaResponse返回值的最佳处理方案》在开发Web应用程序时,我们经常需要通过HTTP请求从服务器获取响应数据,这些数据可以是JSON、XML、甚至是文件,本篇文章将详细解析Java中处理... 目录摘要概述核心问题:关键技术点:源码解析示例 1:使用HttpURLConnection获取Resp