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 docx4j高效处理Word文档的实战指南

《Javadocx4j高效处理Word文档的实战指南》对于需要在Java应用程序中生成、修改或处理Word文档的开发者来说,docx4j是一个强大而专业的选择,下面我们就来看看docx4j的具体使用... 目录引言一、环境准备与基础配置1.1 Maven依赖配置1.2 初始化测试类二、增强版文档操作示例2.

MyBatis-Plus通用中等、大量数据分批查询和处理方法

《MyBatis-Plus通用中等、大量数据分批查询和处理方法》文章介绍MyBatis-Plus分页查询处理,通过函数式接口与Lambda表达式实现通用逻辑,方法抽象但功能强大,建议扩展分批处理及流式... 目录函数式接口获取分页数据接口数据处理接口通用逻辑工具类使用方法简单查询自定义查询方法总结函数式接口

C++中全局变量和局部变量的区别

《C++中全局变量和局部变量的区别》本文主要介绍了C++中全局变量和局部变量的区别,全局变量和局部变量在作用域和生命周期上有显著的区别,下面就来介绍一下,感兴趣的可以了解一下... 目录一、全局变量定义生命周期存储位置代码示例输出二、局部变量定义生命周期存储位置代码示例输出三、全局变量和局部变量的区别作用域

C++中assign函数的使用

《C++中assign函数的使用》在C++标准模板库中,std::list等容器都提供了assign成员函数,它比操作符更灵活,支持多种初始化方式,下面就来介绍一下assign的用法,具有一定的参考价... 目录​1.assign的基本功能​​语法​2. 具体用法示例​​​(1) 填充n个相同值​​(2)

JAVA中安装多个JDK的方法

《JAVA中安装多个JDK的方法》文章介绍了在Windows系统上安装多个JDK版本的方法,包括下载、安装路径修改、环境变量配置(JAVA_HOME和Path),并说明如何通过调整JAVA_HOME在... 首先去oracle官网下载好两个版本不同的jdk(需要登录Oracle账号,没有可以免费注册)下载完

SpringBoot结合Docker进行容器化处理指南

《SpringBoot结合Docker进行容器化处理指南》在当今快速发展的软件工程领域,SpringBoot和Docker已经成为现代Java开发者的必备工具,本文将深入讲解如何将一个SpringBo... 目录前言一、为什么选择 Spring Bootjavascript + docker1. 快速部署与

c++ 类成员变量默认初始值的实现

《c++类成员变量默认初始值的实现》本文主要介绍了c++类成员变量默认初始值,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录C++类成员变量初始化c++类的变量的初始化在C++中,如果使用类成员变量时未给定其初始值,那么它将被

C++中NULL与nullptr的区别小结

《C++中NULL与nullptr的区别小结》本文介绍了C++编程中NULL与nullptr的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编... 目录C++98空值——NULLC++11空值——nullptr区别对比示例 C++98空值——NUL

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window

Python中反转字符串的常见方法小结

《Python中反转字符串的常见方法小结》在Python中,字符串对象没有内置的反转方法,然而,在实际开发中,我们经常会遇到需要反转字符串的场景,比如处理回文字符串、文本加密等,因此,掌握如何在Pyt... 目录python中反转字符串的方法技术背景实现步骤1. 使用切片2. 使用 reversed() 函