转自:http://zxdflyer.blog.163.com/blog/static/25664262201322510217495/

C++标准模板库std使用广泛。该库中处理字符串的对象为std::string,该对象常用来对字符串分割、替换、提取子字符串等操作。但是由于该库全部使用模板编程,而且函数形式也比较复杂,在使用时经常出现问题。为了便于重用,根据在实际使用时常用到的功能,我将相应的代码集成到了一个文件中,代码如下:

 /*********************************************************************************************
* 文件:StringLib
* 功能:基于的std::string实现的常用字符串操作,字符串分割,替换等
* 作者:张晓东* 时间:2012-11-19
* 修改:2012-11-19完成初步版本,实现:字符串分割,字符串替换,提取文件路径,文件名字,文件扩展名*********************************************************************************************/ #ifndef _StringLib_h
#define _StringLib_h
#include <string>
using namespace std;
#ifdef _cplusplusextern "C"
{
#endif
//从字符串str中,使用pattern进行分割,并存储到strVec中
boolStringSplit(std::string src, std::string pattern,
std::vector<std::string>& strVec)
{
std::string::size_type pos;
src +=pattern;//扩展字符串以方便操作
int size=src.size();
for(int i=; i<size; i++)
{
pos = src.find(pattern,i);
if(pos<size)
{
std::string s=src.substr(i,pos-i);
strVec.push_back(s);
i=pos+pattern.size()-;
}
}
return true;
} //将字符串str中的所有target字符串替换为replacement
bool StringReplace(std::string& src, std::string target, std::string replacement){
std::string::size_type startpos = ;
while (startpos!= std::string::npos)
{
startpos = src.find(target);//找到'.'的位置
if( startpos != std::string::npos ) //std::string::npos表示没有找到该字符
{
src.replace(startpos,,replacement); //实施替换,注意后面一定要用""引起来,表示字符串
}
}
return true;
} //提取路径中的文件名字(带路径,不带扩展名)
//substr字符串中,第一个参数为截取的位置,第二个为截取的长度std::stringStringGetFullFileName(std::string path)
{
return path.substr(, path.rfind('.') == std::string::npos ? path.length() : path.rfind('.') );}
//提取路径中的文件名字
std::string StringGetFileName(std::string path)
{ StringReplace(path, "/", "\\");
std::string::size_type startpos = path.rfind('\\') == std::string::npos ? path.length() : path.rfind('\\')+;
std::string::size_type endpos = path.rfind('.') == std::string::npos ? path.length() : path.rfind('.');
return path.substr(startpos, endpos-startpos);
} //提取路径中文件名字(带扩展名)
std::string StringGetFileNameWithExt(std::string path)
{ StringReplace(path, "/", "\\");
std::string::size_type startpos = path.rfind('\\') == std::string::npos ? path.length() : path.rfind('\\')+;
return path.substr(startpos);
} //提取路径中的文件路径
std::string StringGetDirectory(std::string path)
{ StringReplace(path, "/", "\\");
return path.substr(, path.rfind('\\') == std::string::npos ? path.length() : path.rfind('\\') );
} //提取路径中的文件类型
std::string StringGetFileExt(std::string path)
{ StringReplace(path, "/", "\\");
return path.substr(path.rfind('.') == std::string::npos ? path.length() : path.rfind('.')+ );
}
#ifdef _cplusplus
}
#endif
#endif

基于std::string的字符串处理的更多相关文章

  1. VC++ 中使用 std::string 转换字符串编码

    目录 第1章说明    1 1.1 代码    1 1.2 使用    4 第1章说明 VC++中宽窄字符串的相互转换比较麻烦,借助std::string能大大减少代码量. 1.1 代码 函数声明如下 ...

  2. C++ std::string 在一个字符串前插入一个字符串几种方式

    目录 1.直接使用字符串相加 2.使用insert函数 比较:通过Quick C++ Benchmarks 可得到结果 1.直接使用字符串相加 std::string a = "hello& ...

  3. [C/C++] String Reverse 字符串 反转

    #include <iostream> #include <string> #include <algorithm> #include <cstring> ...

  4. std::string在多字节字符集环境下substr的实现方法

    昨天写到<使用多字节字符集的跨平台(PC.Android.IOS.WP)编码/解码方法>中提到服务端使用std::string处理字符串,std::string对多字节字符集支持并不是很完 ...

  5. std::string的Copy-on-Write:不如想象中美好(VC不使用这种方式,而使用对小字符串更友好的SSO实现)

    Copy-on-write(以下简称COW)是一种很重要的优化手段.它的核心思想是懒惰处理多个实体的资源请求,在多个实体之间共享某些资源,直到有实体需要对资源进行修改时,才真正为该实体分配私有的资源. ...

  6. 【超值分享】为何写服务器程序需要自己管理内存,从改造std::string字符串操作说起。。。

    服务器程序为何要进行内存管理,管中窥豹,让我们从string字符串的操作说起...... new/delete是用于c++中的动态内存管理函数,而malloc/free在c++和c中都可以使用,本质上 ...

  7. std::string 字符串替换

    std::string 没有原生的字符串替换函数,需要自己来完成 string& replace_str(string& str, const string& to_repla ...

  8. std::string 字符串切割

    在很多字符串类库里都实现了split函数.不过在std里没有实现.在这里拿出几个: 1. 用单字符作为分隔 #include <string> #include <vector> ...

  9. std::string 字符串大小写转换(转)

    该问题归结为std::transform函数的使用 函数原型 template < class InputIterator, class OutputIterator, class UnaryO ...

随机推荐

  1. oracle 字段自增 两段代码搞定

    (这几天做了个小小课程设计时用的是oracle数据库,第一次用,发现oracle和我们以前用的sql server .mysql是有如此多不同的地方,下面是遇到的问题之一和解决方法,和大家分享下) 用 ...

  2. Vue基础---->vue-router的使用(一)

    用 Vue.js + vue-router 创建单页应用,是非常简单的.使用 Vue.js 时,我们就已经把组件组合成一个应用了,当你要把 vue-router 加进来,只需要配置组件和路由映射,然后 ...

  3. Python学习之k-近邻实例

    海伦收集约会数据巳经有了一段时间,她把这些数据存放在文本文件datingTestSet.txt中,每个样本数据占据一行,总共有 1000 行.海伦的样本主要包含以下 3 种特征: 1. 每年获得的飞行 ...

  4. 转移wordpress到另一台主机

    做项目的代码是两个人,我想把另一个小伙伴做的转移到自己的linux系统上(主要是linux下一片空白,从头做太浪费时间了) 这个过程其实也可以用来类比从本地到服务器的过程(可能略有不同,真上线的时候会 ...

  5. rest_framework之频率详解 03

    访问频率(节流) 1.某个用户一分钟之内访问的次数不能超过3次,超过3次则不能访问了,需要等待,过段时间才能再访问. 2.自定义访问频率.两个方法都必须写上. 登入页面的视图加上访问频率 3.返回值F ...

  6. 01.MyBatis入门

        MyBatis入参考文档:http://mybatis.org/mybatis-3/zh/  1.使用MyBatis前的准备 1.增加Maven依赖 <dependency> &l ...

  7. 解决pip install 安装慢问题

    使用豆瓣源 比如安装pyspark pip install -i https://pypi.douban.com/simple/ pyspark 速度就比用pip install快N倍 关注公众号:

  8. 9.SQL存储过程实例详解

    本文用3个题目,从建立数据库到创建存储过程,详细讲解数据库的功能. 题目1 学校图书馆借书信息管理系统建立三个表:学生信息表:student 字段名称 数据类型 说明 stuID char(10) 学 ...

  9. CentOS7.2升级默认yum安装的php版本

    CentOS7.2yum安装php默认版本为5.4,可以升级通过yum安装更高版本 设置yum源 rpm -Uvh https://mirror.webtatic.com/yum/el7/webtat ...

  10. pta 习题集5-18 打印学生选课清单

    假设全校有最多40000名学生和最多2500门课程.现给出每门课的选课学生名单,要求输出每个前来查询的学生的选课清单. 输入格式: 输入的第一行是两个正整数:N(≤≤40000),为前来查询课表的学生 ...