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

相关文章

无人叉车3d激光slam多房间建图定位异常处理方案-墙体画线地图切分方案

墙体画线地图切分方案 针对问题:墙体两侧特征混淆误匹配,导致建图和定位偏差,表现为过门跳变、外月台走歪等 ·解决思路:预期的根治方案IGICP需要较长时间完成上线,先使用切分地图的工程化方案,即墙体两侧切分为不同地图,在某一侧只使用该侧地图进行定位 方案思路 切分原理:切分地图基于关键帧位置,而非点云。 理论基础:光照是直线的,一帧点云必定只能照射到墙的一侧,无法同时照到两侧实践考虑:关

hdu1043(八数码问题,广搜 + hash(实现状态压缩) )

利用康拓展开将一个排列映射成一个自然数,然后就变成了普通的广搜题。 #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#include<stdlib.h>#include<ctype.h>#inclu

【C++ Primer Plus习题】13.4

大家好,这里是国中之林! ❥前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到网站。有兴趣的可以点点进去看看← 问题: 解答: main.cpp #include <iostream>#include "port.h"int main() {Port p1;Port p2("Abc", "Bcc", 30);std::cout <<

C++包装器

包装器 在 C++ 中,“包装器”通常指的是一种设计模式或编程技巧,用于封装其他代码或对象,使其更易于使用、管理或扩展。包装器的概念在编程中非常普遍,可以用于函数、类、库等多个方面。下面是几个常见的 “包装器” 类型: 1. 函数包装器 函数包装器用于封装一个或多个函数,使其接口更统一或更便于调用。例如,std::function 是一个通用的函数包装器,它可以存储任意可调用对象(函数、函数

C++11第三弹:lambda表达式 | 新的类功能 | 模板的可变参数

🌈个人主页: 南桥几晴秋 🌈C++专栏: 南桥谈C++ 🌈C语言专栏: C语言学习系列 🌈Linux学习专栏: 南桥谈Linux 🌈数据结构学习专栏: 数据结构杂谈 🌈数据库学习专栏: 南桥谈MySQL 🌈Qt学习专栏: 南桥谈Qt 🌈菜鸡代码练习: 练习随想记录 🌈git学习: 南桥谈Git 🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈🌈�

【C++】_list常用方法解析及模拟实现

相信自己的力量,只要对自己始终保持信心,尽自己最大努力去完成任何事,就算事情最终结果是失败了,努力了也不留遗憾。💓💓💓 目录   ✨说在前面 🍋知识点一:什么是list? •🌰1.list的定义 •🌰2.list的基本特性 •🌰3.常用接口介绍 🍋知识点二:list常用接口 •🌰1.默认成员函数 🔥构造函数(⭐) 🔥析构函数 •🌰2.list对象

【Prometheus】PromQL向量匹配实现不同标签的向量数据进行运算

✨✨ 欢迎大家来到景天科技苑✨✨ 🎈🎈 养成好习惯,先赞后看哦~🎈🎈 🏆 作者简介:景天科技苑 🏆《头衔》:大厂架构师,华为云开发者社区专家博主,阿里云开发者社区专家博主,CSDN全栈领域优质创作者,掘金优秀博主,51CTO博客专家等。 🏆《博客》:Python全栈,前后端开发,小程序开发,人工智能,js逆向,App逆向,网络系统安全,数据分析,Django,fastapi

让树莓派智能语音助手实现定时提醒功能

最初的时候是想直接在rasa 的chatbot上实现,因为rasa本身是带有remindschedule模块的。不过经过一番折腾后,忽然发现,chatbot上实现的定时,语音助手不一定会有响应。因为,我目前语音助手的代码设置了长时间无应答会结束对话,这样一来,chatbot定时提醒的触发就不会被语音助手获悉。那怎么让语音助手也具有定时提醒功能呢? 我最后选择的方法是用threading.Time

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

06 C++Lambda表达式

lambda表达式的定义 没有显式模版形参的lambda表达式 [捕获] 前属性 (形参列表) 说明符 异常 后属性 尾随类型 约束 {函数体} 有显式模版形参的lambda表达式 [捕获] <模版形参> 模版约束 前属性 (形参列表) 说明符 异常 后属性 尾随类型 约束 {函数体} 含义 捕获:包含零个或者多个捕获符的逗号分隔列表 模板形参:用于泛型lambda提供个模板形参的名