C++ 字符串处理4-根据指定的分隔符将字符串分割为多个子串根据指定的分隔符将多个子串连接成一个字符串

2024-06-14 08:04

本文主要是介绍C++ 字符串处理4-根据指定的分隔符将字符串分割为多个子串根据指定的分隔符将多个子串连接成一个字符串,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. 关键词

C++ 字符串处理 分割字符串 连接字符串 跨平台

2. strutil.h

#pragma once#include <string>
#include <vector>namespace cutl
{/*** @brief The type of vector strings used in this library.**/using strvec = std::vector<std::string>;/*** @brief Split a string into a vector of substrings using a given separator.** @param str the string to be split.* @param separator the separator to split the string.* @return strvec the vector of substrings.*/strvec split(const std::string &str, const std::string &separator);/*** @brief Join a vector of strings into a single string using a given separator.** @param strlist the vector of strings to be joined.* @param separator the separator to join the strings.* @return std::string the joined string.*/std::string join(const strvec &strlist, const std::string &separator = "");
} // namespace cutl

3. strutil.cpp

#include <cctype>
#include <algorithm>
#include "strutil.h"namespace cutl
{strvec split(const std::string &str, const std::string &pattern){strvec res;if (str == "")return res;// 在字符串末尾也加入分隔符,方便截取最后一段std::string strs = str + pattern;size_t pos = strs.find(pattern);int startIndex = 0;while (pos != strs.npos){std::string temp = strs.substr(startIndex, pos - startIndex);res.emplace_back(temp);startIndex = pos + 1;pos = strs.find(pattern, startIndex);}return res;}std::string join(const strvec &strlist, const std::string &separator){std::string text;for (size_t i = 0; i < strlist.size(); i++){text += strlist[i];if (i < strlist.size() - 1){text += separator;}}return text;}
} // namespace cutl

4. 测试代码

#include "common.hpp"
#include "strutil.h"void TestJoinSplit()
{PrintSubTitle("TestJoinSplit");// splitstd::string fruits = "apple, banana, orange, pear";std::cout << "fruits: " << fruits << std::endl;auto fruits_vec = cutl::split(fruits, ",");std::cout << "list fruits item:" << std::endl;for (size_t i = 0; i < fruits_vec.size(); i++){auto item = fruits_vec[i];auto fruit = cutl::strip(item);std::cout << item << ", after strip:" << fruit << std::endl;fruits_vec[i] = fruit;}// joinstd::cout << "join fruits with comma: " << cutl::join(fruits_vec, "; ") << std::endl;
}

5. 运行结果

-------------------------------------------TestJoinSplit--------------------------------------------
fruits: apple, banana, orange, pear
list fruits item:
apple, after strip:applebanana, after strip:bananaorange, after strip:orangepear, after strip:pear
join fruits with comma: apple; banana; orange; pear

6. 源码地址

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

这篇关于C++ 字符串处理4-根据指定的分隔符将字符串分割为多个子串根据指定的分隔符将多个子串连接成一个字符串的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Java中的StringBuilder之如何高效构建字符串

《Java中的StringBuilder之如何高效构建字符串》本文将深入浅出地介绍StringBuilder的使用方法、性能优势以及相关字符串处理技术,结合代码示例帮助读者更好地理解和应用,希望对大家... 目录关键点什么是 StringBuilder?为什么需要 StringBuilder?如何使用 St

C#如何调用C++库

《C#如何调用C++库》:本文主要介绍C#如何调用C++库方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录方法一:使用P/Invoke1. 导出C++函数2. 定义P/Invoke签名3. 调用C++函数方法二:使用C++/CLI作为桥接1. 创建C++/CL

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

python处理带有时区的日期和时间数据

《python处理带有时区的日期和时间数据》这篇文章主要为大家详细介绍了如何在Python中使用pytz库处理时区信息,包括获取当前UTC时间,转换为特定时区等,有需要的小伙伴可以参考一下... 目录时区基本信息python datetime使用timezonepandas处理时区数据知识延展时区基本信息

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

一文详解Java异常处理你都了解哪些知识

《一文详解Java异常处理你都了解哪些知识》:本文主要介绍Java异常处理的相关资料,包括异常的分类、捕获和处理异常的语法、常见的异常类型以及自定义异常的实现,文中通过代码介绍的非常详细,需要的朋... 目录前言一、什么是异常二、异常的分类2.1 受检异常2.2 非受检异常三、异常处理的语法3.1 try-

Java中字符串转时间与时间转字符串的操作详解

《Java中字符串转时间与时间转字符串的操作详解》Java的java.time包提供了强大的日期和时间处理功能,通过DateTimeFormatter可以轻松地在日期时间对象和字符串之间进行转换,下面... 目录一、字符串转时间(一)使用预定义格式(二)自定义格式二、时间转字符串(一)使用预定义格式(二)自

Python使用getopt处理命令行参数示例解析(最佳实践)

《Python使用getopt处理命令行参数示例解析(最佳实践)》getopt模块是Python标准库中一个简单但强大的命令行参数处理工具,它特别适合那些需要快速实现基本命令行参数解析的场景,或者需要... 目录为什么需要处理命令行参数?getopt模块基础实际应用示例与其他参数处理方式的比较常见问http

Java Response返回值的最佳处理方案

《JavaResponse返回值的最佳处理方案》在开发Web应用程序时,我们经常需要通过HTTP请求从服务器获取响应数据,这些数据可以是JSON、XML、甚至是文件,本篇文章将详细解析Java中处理... 目录摘要概述核心问题:关键技术点:源码解析示例 1:使用HttpURLConnection获取Resp