C++ 字符串处理3-实现starts_with和ends_with的字符串判断功能

2024-06-14 17:44

本文主要是介绍C++ 字符串处理3-实现starts_with和ends_with的字符串判断功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  • 1. 关键词
  • 2. C++20及之后
  • 3. C++20之前
    • 3.1. strutil.h
    • 3.2. strutil.cpp
    • 3.3. 测试代码
    • 3.4. 运行结果
    • 3.5. 源码地址

1. 关键词

C++ 字符串处理 starts_with ends_with std::string 跨平台

2. C++20及之后

C++20标准开始,STL已经提供了starts_withends_with函数,可以直接使用。在 <string>头文件中,可以直接作为字符串对象的成员函数使用。

官方API文档: https://en.cppreference.com/w/cpp/string/basic_string

3. C++20之前

C++20之前, STL本身不支持starts_with和ends_with的功能,开发者需要自己实现,可参考以下实现代码。

3.1. strutil.h

#include <string>
namespace cutl
{/*** @brief Check if a string starts with a given substring.** @param str the string to be checked.* @param start the substring to be checked.* @param ignoreCase whether to ignore case when comparing, default is false.* @return true if the string starts with the substring, false otherwise.*/bool starts_with(const std::string &str, const std::string &start, bool ignoreCase = false);/*** @brief Check if a string ends with a given substring.** @param str the string to be checked.* @param end the substring to be checked.* @param ignoreCase whether to ignore case when comparing, default is false.* @return true if the string ends with the substring, false otherwise.*/bool ends_with(const std::string &str, const std::string &end, bool ignoreCase = false);
} // namespace cutl

3.2. strutil.cpp

#include <cctype>
#include <algorithm>
#include "strutil.h"namespace cutl
{bool starts_with(const std::string &str, const std::string &start, bool ignoreCase){int srclen = str.size();int startlen = start.size();if (srclen < startlen){return false;}std::string temp = str.substr(0, startlen);if (ignoreCase){return to_lower(temp) == to_lower(start);}else{return temp == start;}}bool ends_with(const std::string &str, const std::string &end, bool ignoreCase){int srclen = str.size();int endlen = end.size();if (srclen < endlen){return false;}std::string temp = str.substr(srclen - endlen, endlen);if (ignoreCase){return to_lower(temp) == to_lower(end);}else{return temp == end;}}
} // namespace cutl

3.3. 测试代码

#include "common.hpp"
#include "strutil.h"void TestStartswithEndswith()
{PrintSubTitle("TestStartswithEndswith");std::string str1 = "Hello, world!";std::string str2 = "GOODBYE, WORLD!";std::cout << str1 << " start with hello : " << cutl::starts_with(str1, "hello") << std::endl;std::cout << str1 << " start with hello ignoreCase: " << cutl::starts_with(str1, "hello", true) << std::endl;std::cout << str2 << " end with world! : " << cutl::ends_with(str2, "world!") << std::endl;std::cout << str2 << " end with world! ignoreCase: " << cutl::ends_with(str2, "world!", true) << std::endl;
}

3.4. 运行结果

---------------------------------------TestStartswithEndswith---------------------------------------
Hello, world! start with hello : 0
Hello, world! start with hello ignoreCase: 1
GOODBYE, WORLD! end with world! : 0
GOODBYE, WORLD! end with world! ignoreCase: 1

3.5. 源码地址

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

这篇关于C++ 字符串处理3-实现starts_with和ends_with的字符串判断功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Nginx实现高并发的项目实践

《Nginx实现高并发的项目实践》本文主要介绍了Nginx实现高并发的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录使用最新稳定版本的Nginx合理配置工作进程(workers)配置工作进程连接数(worker_co

python中列表list切分的实现

《python中列表list切分的实现》列表是Python中最常用的数据结构之一,经常需要对列表进行切分操作,本文主要介绍了python中列表list切分的实现,文中通过示例代码介绍的非常详细,对大家... 目录一、列表切片的基本用法1.1 基本切片操作1.2 切片的负索引1.3 切片的省略二、列表切分的高

基于Python实现一个PDF特殊字体提取工具

《基于Python实现一个PDF特殊字体提取工具》在PDF文档处理场景中,我们常常需要针对特定格式的文本内容进行提取分析,本文介绍的PDF特殊字体提取器是一款基于Python开发的桌面应用程序感兴趣的... 目录一、应用背景与功能概述二、技术架构与核心组件2.1 技术选型2.2 系统架构三、核心功能实现解析

C++ Primer 标准库vector示例详解

《C++Primer标准库vector示例详解》该文章主要介绍了C++标准库中的vector类型,包括其定义、初始化、成员函数以及常见操作,文章详细解释了如何使用vector来存储和操作对象集合,... 目录3.3标准库Vector定义和初始化vector对象通列表初始化vector对象创建指定数量的元素值

使用Python实现表格字段智能去重

《使用Python实现表格字段智能去重》在数据分析和处理过程中,数据清洗是一个至关重要的步骤,其中字段去重是一个常见且关键的任务,下面我们看看如何使用Python进行表格字段智能去重吧... 目录一、引言二、数据重复问题的常见场景与影响三、python在数据清洗中的优势四、基于Python的表格字段智能去重

Spring AI集成DeepSeek实现流式输出的操作方法

《SpringAI集成DeepSeek实现流式输出的操作方法》本文介绍了如何在SpringBoot中使用Sse(Server-SentEvents)技术实现流式输出,后端使用SpringMVC中的S... 目录一、后端代码二、前端代码三、运行项目小天有话说题外话参考资料前面一篇文章我们实现了《Spring

Nginx中location实现多条件匹配的方法详解

《Nginx中location实现多条件匹配的方法详解》在Nginx中,location指令用于匹配请求的URI,虽然location本身是基于单一匹配规则的,但可以通过多种方式实现多个条件的匹配逻辑... 目录1. 概述2. 实现多条件匹配的方式2.1 使用多个 location 块2.2 使用正则表达式

使用Apache POI在Java中实现Excel单元格的合并

《使用ApachePOI在Java中实现Excel单元格的合并》在日常工作中,Excel是一个不可或缺的工具,尤其是在处理大量数据时,本文将介绍如何使用ApachePOI库在Java中实现Excel... 目录工具类介绍工具类代码调用示例依赖配置总结在日常工作中,Excel 是一个不可或缺的工http://

SpringBoot实现导出复杂对象到Excel文件

《SpringBoot实现导出复杂对象到Excel文件》这篇文章主要为大家详细介绍了如何使用Hutool和EasyExcel两种方式来实现在SpringBoot项目中导出复杂对象到Excel文件,需要... 在Spring Boot项目中导出复杂对象到Excel文件,可以利用Hutool或EasyExcel

Python如何实现读取csv文件时忽略文件的编码格式

《Python如何实现读取csv文件时忽略文件的编码格式》我们再日常读取csv文件的时候经常会发现csv文件的格式有多种,所以这篇文章为大家介绍了Python如何实现读取csv文件时忽略文件的编码格式... 目录1、背景介绍2、库的安装3、核心代码4、完整代码1、背景介绍我们再日常读取csv文件的时候经常