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

相关文章

Python函数的基本用法、返回值特性、全局变量修改及异常处理技巧

《Python函数的基本用法、返回值特性、全局变量修改及异常处理技巧》本文将通过实际代码示例,深入讲解Python函数的基本用法、返回值特性、全局变量修改以及异常处理技巧,感兴趣的朋友跟随小编一起看看... 目录一、python函数定义与调用1.1 基本函数定义1.2 函数调用二、函数返回值详解2.1 有返

Python Excel 通用筛选函数的实现

《PythonExcel通用筛选函数的实现》本文主要介绍了PythonExcel通用筛选函数的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着... 目录案例目的示例数据假定数据来源是字典优化:通用CSV数据处理函数使用说明使用示例注意事项案例目的第一

C#使用SendMessage实现进程间通信的示例代码

《C#使用SendMessage实现进程间通信的示例代码》在软件开发中,进程间通信(IPC)是关键技术之一,C#通过调用WindowsAPI的SendMessage函数实现这一功能,本文将通过实例介绍... 目录第一章:SendMessage的底层原理揭秘第二章:构建跨进程通信桥梁2.1 定义通信协议2.2

JAVA实现亿级千万级数据顺序导出的示例代码

《JAVA实现亿级千万级数据顺序导出的示例代码》本文主要介绍了JAVA实现亿级千万级数据顺序导出的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 前提:主要考虑控制内存占用空间,避免出现同时导出,导致主程序OOM问题。实现思路:A.启用线程池

Python实现中文大写金额转阿拉伯数字

《Python实现中文大写金额转阿拉伯数字》在财务票据中,中文大写金额被广泛使用以防止篡改,但在数据处理时,我们需要将其转换为阿拉伯数字形式,下面我们就来看看如何使用Python实现这一转换吧... 目录一、核心思路拆解二、中文数字解析实现三、大单位分割策略四、元角分综合处理五、测试验证六、全部代码在财务票

java 恺撒加密/解密实现原理(附带源码)

《java恺撒加密/解密实现原理(附带源码)》本文介绍Java实现恺撒加密与解密,通过固定位移量对字母进行循环替换,保留大小写及非字母字符,由于其实现简单、易于理解,恺撒加密常被用作学习加密算法的入... 目录Java 恺撒加密/解密实现1. 项目背景与介绍2. 相关知识2.1 恺撒加密算法原理2.2 Ja

React 记忆缓存的三种方法实现

《React记忆缓存的三种方法实现》本文主要介绍了React记忆缓存的三种方法实现,包含React.memo、useMemo、useCallback,用于避免不必要的组件重渲染和计算,感兴趣的可以... 目录1. React.memo2. useMemo3. useCallback使用场景与注意事项在 Re

Nginx实现端口映射的示例代码

《Nginx实现端口映射的示例代码》本文主要介绍了Nginx实现端口映射的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录1. 找到nginx的部署路径2. 备份原来的配置文件3. 编辑nginx.conf文件4. 在

Java StringBuilder 实现原理全攻略

《JavaStringBuilder实现原理全攻略》StringBuilder是Java提供的可变字符序列类,位于java.lang包中,专门用于高效处理字符串的拼接和修改操作,本文给大家介绍Ja... 目录一、StringBuilder 基本概述核心特性二、StringBuilder 核心实现2.1 内部

Android实现图片浏览功能的示例详解(附带源码)

《Android实现图片浏览功能的示例详解(附带源码)》在许多应用中,都需要展示图片并支持用户进行浏览,本文主要为大家介绍了如何通过Android实现图片浏览功能,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码