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对象和JSON字符串之间的转换方法(全网最清晰)

《Java对象和JSON字符串之间的转换方法(全网最清晰)》:本文主要介绍如何在Java中使用Jackson库将对象转换为JSON字符串,并提供了一个简单的工具类示例,该工具类支持基本的转换功能,... 目录前言1. 引入 Jackson 依赖2. 创建 jsON 工具类3. 使用示例转换 Java 对象为

Python自动化处理手机验证码

《Python自动化处理手机验证码》手机验证码是一种常见的身份验证手段,广泛应用于用户注册、登录、交易确认等场景,下面我们来看看如何使用Python自动化处理手机验证码吧... 目录一、获取手机验证码1.1 通过短信接收验证码1.2 使用第三方短信接收服务1.3 使用ADB读取手机短信1.4 通过API获取

golang字符串匹配算法解读

《golang字符串匹配算法解读》文章介绍了字符串匹配算法的原理,特别是Knuth-Morris-Pratt(KMP)算法,该算法通过构建模式串的前缀表来减少匹配时的不必要的字符比较,从而提高效率,在... 目录简介KMP实现代码总结简介字符串匹配算法主要用于在一个较长的文本串中查找一个较短的字符串(称为

Python自动化Office文档处理全攻略

《Python自动化Office文档处理全攻略》在日常办公中,处理Word、Excel和PDF等Office文档是再常见不过的任务,手动操作这些文档不仅耗时耗力,还容易出错,幸运的是,Python提供... 目录一、自动化处理Word文档1. 安装python-docx库2. 读取Word文档内容3. 修改

Python自动化办公之合并多个Excel

《Python自动化办公之合并多个Excel》在日常的办公自动化工作中,尤其是处理大量数据时,合并多个Excel表格是一个常见且繁琐的任务,下面小编就来为大家介绍一下如何使用Python轻松实现合... 目录为什么选择 python 自动化目标使用 Python 合并多个 Excel 文件安装所需库示例代码

C++一个数组赋值给另一个数组方式

《C++一个数组赋值给另一个数组方式》文章介绍了三种在C++中将一个数组赋值给另一个数组的方法:使用循环逐个元素赋值、使用标准库函数std::copy或std::memcpy以及使用标准库容器,每种方... 目录C++一个数组赋值给另一个数组循环遍历赋值使用标准库中的函数 std::copy 或 std::

C++使用栈实现括号匹配的代码详解

《C++使用栈实现括号匹配的代码详解》在编程中,括号匹配是一个常见问题,尤其是在处理数学表达式、编译器解析等任务时,栈是一种非常适合处理此类问题的数据结构,能够精确地管理括号的匹配问题,本文将通过C+... 目录引言问题描述代码讲解代码解析栈的状态表示测试总结引言在编程中,括号匹配是一个常见问题,尤其是在

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D