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

相关文章

SpringBoot结合Docker进行容器化处理指南

《SpringBoot结合Docker进行容器化处理指南》在当今快速发展的软件工程领域,SpringBoot和Docker已经成为现代Java开发者的必备工具,本文将深入讲解如何将一个SpringBo... 目录前言一、为什么选择 Spring Bootjavascript + docker1. 快速部署与

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被

C++中NULL与nullptr的区别小结

《C++中NULL与nullptr的区别小结》本文介绍了C++编程中NULL与nullptr的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编... 目录C++98空值——NULLC++11空值——nullptr区别对比示例 C++98空值——NUL

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window

Python中反转字符串的常见方法小结

《Python中反转字符串的常见方法小结》在Python中,字符串对象没有内置的反转方法,然而,在实际开发中,我们经常会遇到需要反转字符串的场景,比如处理回文字符串、文本加密等,因此,掌握如何在Pyt... 目录python中反转字符串的方法技术背景实现步骤1. 使用切片2. 使用 reversed() 函

Python使用vllm处理多模态数据的预处理技巧

《Python使用vllm处理多模态数据的预处理技巧》本文深入探讨了在Python环境下使用vLLM处理多模态数据的预处理技巧,我们将从基础概念出发,详细讲解文本、图像、音频等多模态数据的预处理方法,... 目录1. 背景介绍1.1 目的和范围1.2 预期读者1.3 文档结构概述1.4 术语表1.4.1 核

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

C++20管道运算符的实现示例

《C++20管道运算符的实现示例》本文简要介绍C++20管道运算符的使用与实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录标准库的管道运算符使用自己实现类似的管道运算符我们不打算介绍太多,因为它实际属于c++20最为重要的