转自: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. 第五篇:浅谈CPU 并行编程和 GPU 并行编程的区别

    前言 CPU 的并行编程技术,也是高性能计算中的热点,也是今后要努力学习的方向.那么它和 GPU 并行编程有何区别呢? 本文将做出详细的对比,分析各自的特点,为将来深入学习 CPU 并行编程技术打下铺 ...

  2. 虚拟机如何装LINUX

    VMware 提供了免費的虛擬機 VMware player 5.0.2 供使用者下載. 從 VMware 官網http://www.vmware.com/. 的頁面進入 “Products”  “ ...

  3. Effective C++ —— 继承与面向对象设计(六)

    条款32 : 确定你的public继承塑模出is-a关系 以C++进行面向对象编程,最重要的一个规则是:public inheritance(公开继承)意味“is-a”(是一种)的关系.请务必牢记.当 ...

  4. ubuntu下Eclipse创建Django项目

    (注:部分过程可能需要FQ) Eclipse版本:Mars.x 点击help->Eclipse Marketplace,搜索“PyDev”并下载安装. 然后选择window->prefer ...

  5. RDO部署多节点OpenStack Havana(OVS+GRE)

    RDO是由红帽RedHat公司推出的部署OpenStack集群的一个基于Puppet的部署工具,可以很快地通过RDO部署一套复杂的OpenStack环境,当前的RDO默认情况下,使用Neutron进行 ...

  6. ubuntu14.04 LTS Visual Studio Code 编辑器推荐

    除了ubuntu geany (茶壶图标) 这个一直爱好的编辑器,发现一个新的编辑器“Visual Studio Code”,也是很好用,记录下 https://code.visualstudio.c ...

  7. JavaWeb温习之HttpServletResquest对象

    HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象提供的方法,可以获得客户端请求的所有信息. 1 ...

  8. executeQuery、executeUpdate 和 execute

    Statement 接口提供了三种执行 SQL 语句的方法:executeQuery.executeUpdate 和 execute.使用哪一个方法由 SQL 语句所产生的内容决定. 1. Resul ...

  9. 关于PreparedStatement.addBatch()方法 (转)

    Statement和PreparedStatement的区别就不多废话了,直接说PreparedStatement最重要的addbatch()结构的使用. 1.建立链接,(打电话拨号 ) Connec ...

  10. Windows Phone 在读取网络图片之前先显示默认图片

    1.新建一个控件WindowsPhoneControl1 WindowsPhoneControl1.xaml <UserControl x:Class="DefaultImage.Wi ...