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实现字符串大小写转换的常用方法

《Java实现字符串大小写转换的常用方法》在Java中,字符串大小写转换是文本处理的核心操作之一,Java提供了多种灵活的方式来实现大小写转换,适用于不同场景和需求,本文将全面解析大小写转换的各种方法... 目录前言核心转换方法1.String类的基础方法2. 考虑区域设置的转换3. 字符级别的转换高级转换

C++ move 的作用详解及陷阱最佳实践

《C++move的作用详解及陷阱最佳实践》文章详细介绍了C++中的`std::move`函数的作用,包括为什么需要它、它的本质、典型使用场景、以及一些常见陷阱和最佳实践,感兴趣的朋友跟随小编一起看... 目录C++ move 的作用详解一、一句话总结二、为什么需要 move?C++98/03 的痛点⚡C++

Python+FFmpeg实现视频自动化处理的完整指南

《Python+FFmpeg实现视频自动化处理的完整指南》本文总结了一套在Python中使用subprocess.run调用FFmpeg进行视频自动化处理的解决方案,涵盖了跨平台硬件加速、中间素材处理... 目录一、 跨平台硬件加速:统一接口设计1. 核心映射逻辑2. python 实现代码二、 中间素材处

MySQL字符串转数值的方法全解析

《MySQL字符串转数值的方法全解析》在MySQL开发中,字符串与数值的转换是高频操作,本文从隐式转换原理、显式转换方法、典型场景案例、风险防控四个维度系统梳理,助您精准掌握这一核心技能,需要的朋友可... 目录一、隐式转换:自动但需警惕的&ld编程quo;双刃剑”二、显式转换:三大核心方法详解三、典型场景

Go异常处理、泛型和文件操作实例代码

《Go异常处理、泛型和文件操作实例代码》Go语言的异常处理机制与传统的面向对象语言(如Java、C#)所使用的try-catch结构有所不同,它采用了自己独特的设计理念和方法,:本文主要介绍Go异... 目录一:异常处理常见的异常处理向上抛中断程序恢复程序二:泛型泛型函数泛型结构体泛型切片泛型 map三:文

详解C++ 存储二进制数据容器的几种方法

《详解C++存储二进制数据容器的几种方法》本文主要介绍了详解C++存储二进制数据容器,包括std::vector、std::array、std::string、std::bitset和std::ve... 目录1.std::vector<uint8_t>(最常用)特点:适用场景:示例:2.std::arra

C++构造函数中explicit详解

《C++构造函数中explicit详解》explicit关键字用于修饰单参数构造函数或可以看作单参数的构造函数,阻止编译器进行隐式类型转换或拷贝初始化,本文就来介绍explicit的使用,感兴趣的可以... 目录1. 什么是explicit2. 隐式转换的问题3.explicit的使用示例基本用法多参数构造

C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解

《C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解》:本文主要介绍C++,C#,Rust,Go,Java,Python,JavaScript性能对比全面... 目录编程语言性能对比、核心优势与最佳使用场景性能对比表格C++C#RustGoJavapythonjav

C++打印 vector的几种方法小结

《C++打印vector的几种方法小结》本文介绍了C++中遍历vector的几种方法,包括使用迭代器、auto关键字、typedef、计数器以及C++11引入的范围基础循环,具有一定的参考价值,感兴... 目录1. 使用迭代器2. 使用 auto (C++11) / typedef / type alias

SpringSecurity中的跨域问题处理方案

《SpringSecurity中的跨域问题处理方案》本文介绍了跨域资源共享(CORS)技术在JavaEE开发中的应用,详细讲解了CORS的工作原理,包括简单请求和非简单请求的处理方式,本文结合实例代码... 目录1.什么是CORS2.简单请求3.非简单请求4.Spring跨域解决方案4.1.@CrossOr