基于std::string的字符串处理
转自: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的字符串处理的更多相关文章
- VC++ 中使用 std::string 转换字符串编码
目录 第1章说明 1 1.1 代码 1 1.2 使用 4 第1章说明 VC++中宽窄字符串的相互转换比较麻烦,借助std::string能大大减少代码量. 1.1 代码 函数声明如下 ...
- C++ std::string 在一个字符串前插入一个字符串几种方式
目录 1.直接使用字符串相加 2.使用insert函数 比较:通过Quick C++ Benchmarks 可得到结果 1.直接使用字符串相加 std::string a = "hello& ...
- [C/C++] String Reverse 字符串 反转
#include <iostream> #include <string> #include <algorithm> #include <cstring> ...
- std::string在多字节字符集环境下substr的实现方法
昨天写到<使用多字节字符集的跨平台(PC.Android.IOS.WP)编码/解码方法>中提到服务端使用std::string处理字符串,std::string对多字节字符集支持并不是很完 ...
- std::string的Copy-on-Write:不如想象中美好(VC不使用这种方式,而使用对小字符串更友好的SSO实现)
Copy-on-write(以下简称COW)是一种很重要的优化手段.它的核心思想是懒惰处理多个实体的资源请求,在多个实体之间共享某些资源,直到有实体需要对资源进行修改时,才真正为该实体分配私有的资源. ...
- 【超值分享】为何写服务器程序需要自己管理内存,从改造std::string字符串操作说起。。。
服务器程序为何要进行内存管理,管中窥豹,让我们从string字符串的操作说起...... new/delete是用于c++中的动态内存管理函数,而malloc/free在c++和c中都可以使用,本质上 ...
- std::string 字符串替换
std::string 没有原生的字符串替换函数,需要自己来完成 string& replace_str(string& str, const string& to_repla ...
- std::string 字符串切割
在很多字符串类库里都实现了split函数.不过在std里没有实现.在这里拿出几个: 1. 用单字符作为分隔 #include <string> #include <vector> ...
- std::string 字符串大小写转换(转)
该问题归结为std::transform函数的使用 函数原型 template < class InputIterator, class OutputIterator, class UnaryO ...
随机推荐
- canvas二:绘制圆和其他曲线
1.绘制圆 绘制圆是canvas里面不可缺少的功课,而且绘制圆在canvas中的用处很多,好嘞,开扯 绘制圆需要用到arc这个方法: arc(X坐标,Y坐标,半径,起始弧度,结束弧度,旋转方向): 弧 ...
- 安装postgresql之后为什么找不到postgresql service
没有正常启动 postgresql service.可以 在运行里面 输入 services.msc 找到 postgresql 的服务.启动他.或者也可以用postgres 自带的 工具pg_ctl ...
- Gamma编码及Delta编码概述
一.Elias Gamma Coding 即Gamma编码,是一种对正整数进行编码的统一编码,由Peter Elias发明.适用于预先无法获知最大编码整数的情况,而且小整数出现频率高,大整数出现频率低 ...
- windbg载入目标模块pdb
.reload /f xxxx.dll ld xxxx 以kdcom为例子 .reload /f kdcom.dll ld kdcom 二选一
- poj_1151 线段树
题目大意 在平面上给定n个矩形,可以相互覆盖全部或者部分,求出矩形占据的总面积. 题目分析 将矩形按照x方向的进行分割之后,将平面沿着y方向划分一系列单元(不定高度),每个矩形在y方向上占据若干连续的 ...
- java基础---->多线程之ThreadLocal(七)
这里学习一下java多线程中的关于ThreadLocal的用法.人时已尽,人世还长,我在中间,应该休息. ThreadLocal的简单实例 一.ThreadLocal的简单使用 package com ...
- LeetCode——Reverse Linked List
反转链表,用了一个比较笨的方法. public class Solution { public ListNode reverseList(ListNode head) { if(head == nul ...
- 域名绑定和域名解析(DNS)有什么不同?(转载)
域名解析在DNS处设置,DNS服务器将你的域名指向你的存储网页的服务器. 域名绑定在服务器中设置,存储你网页文件的服务器绑定了你的域名才能把浏览者引导到这个域名指定的物理位置来访问. 比如,你进一个高 ...
- hihocoder [Offer收割]编程练习赛14 可疑的记录
题目3 : 可疑的记录 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi有一棵N个节点的树,编号1-N,其中1号节点是整棵树的根.他把这棵树的N-1条边记录成N-1 ...
- linux awk时间计算脚本
在linux如果计划时间是个麻烦事, 用awk脚本如下 BEGIN {FS=":";OFS=":"} {total_seconds=total_seconds+ ...