转自:http://dewei.iteye.com/blog/1566734

//标准C++ string 去除首尾空白字符 2012-8-12 By Dewei
static inline void stringTrim(string &str)
{
//去除左侧空白符
for (int i = ; i != str.length(); ++i) {
char tmp = str[i];
if (!isspace(tmp)) {
str = str.c_str() + i;
break;
}
}
//去除右侧空白符
for (int i = str.length() - ; i != ; --i) {
char tmp = str[i];
if (!isspace(tmp)) {
str.resize(i+);
break;
}
}
} //用分隔符将数组合并为字符串 2012-8-12 by Dewei
//用法:typedef vector<string> stringArray;
string implode(string delimter, stringArray& str_array)
{
string str;
int num_count = str_array.size();
for (int i = ; i < num_count; ++i) {
if (!str_array[i].empty())
str.append(str_array[i]);
if (i < num_count -)
str.append(delimter);
}
return str;
}
//将字符串转换成数组(支持值为空) 2012-8-12 by Dewei
//用法:typedef vector<string> stringArray;
void explode(const string &delimter, string str_source, stringArray &str_array)
{
str_array.clear();
if (delimter.empty() || str_source.empty())
return;
string str_tmp;
string::size_type num_last_pos = ;
string::size_type num_pos = ;
while (num_pos != string::npos) {
num_pos = str_source.find(delimter, num_last_pos);
if(num_pos != string::npos) {
str_tmp = str_source.substr(num_last_pos, num_pos - num_last_pos);
num_last_pos = num_pos + delimter.length();
}
else {
str_tmp = str_source.substr(num_last_pos, str_source.length() - num_last_pos);
}
stringTrim(str_tmp);
str_array.push_back(str_tmp);
}
} //标准C++ std::string 仿CString 替换字符串 by Dewei 2012-6-24
//用法:using namespace std;
//source_str = str_replace(oldstr, newstr, source_str);
static inline string str_replace(const string oldstr, const string newstr, string source_str)
{
string::size_type num_pos = ;
num_pos = source_str.find(oldstr);
while (num_pos != string::npos) {
source_str.replace(num_pos, oldstr.length(), newstr);
num_pos = source_str.find(oldstr, num_pos+oldstr.length());
}
return source_str;
}
//CString 截取指字区域内字符串 2012-6-6 By Dewei
//CString strSrc(_T("http://download.csdn.net/download/lindao0/242800"));
//CString strNew;
//strNew = substr(strSrc, "//", "/");
// CString substr(CString strSrc, const CString strStart, const CString strEnd)
{
int iStart = , iEnd = ;
CString sSub = "";
iStart = strSrc.Find(strStart) + lstrlen(strStart) ;
if (iStart != -) {
sSub = strSrc.Mid(iStart);
iEnd = sSub.Find(strEnd);
if (iEnd != -)
{
sSub = sSub.Left(iEnd);
}
}
return sSub;
} //标准C++ 截取指字区域内字符串 2012-6-23 By Dewei
#include <string>
using std::string; //string strSrc("http://download.csdn.net/download/lindao0/242800");
//string strNew;
//strNew = substr(strSrc, "//", "/");
//printf("%s", strNew.c_str()); string substr(string strSrc, const string strStart, const string strEnd)
{
int iStart = , iEnd = ;
string sSub = "";
iStart = strSrc.find(strStart) + strStart.size();
if (iStart != -) {
sSub = strSrc.substr(iStart);
iEnd = sSub.find(strEnd);
if (iEnd != -) {
return sSub.substr(, iEnd);
}
}
return sSub;
} //标准C++ 无返回值 截取指字区域内字符串 2012-6-23 By Dewei
#include <string>
using std::string; //string strSrc("http://download.csdn.net/download/lindao0/242800");
//char out[1024] = {0};
//substr(strSrc, "//", "/", out);
//printf("%s", out);
void substr(string &strSrc, const string &strStart, const string &strEnd, char *out)
{
int iStart = , iEnd = ;
string sSub = "";
iStart = strSrc.find(strStart) + strStart.size();
if (iStart != -) {
sSub = strSrc.substr(iStart);
iEnd = sSub.find(strEnd);
if (iEnd != -) {
sSub = sSub.substr(, iEnd);
strcpy(out, sSub.c_str());
}
}
}

标准C++ 字符串处理增强函数的更多相关文章

  1. Python标准库:内置函数hasattr(object, name)

    Python标准库:内置函数hasattr(object, name) 本函数是用来判断对象object的属性(name表示)是否存在.如果属性(name表示)存在,则返回True,否则返回False ...

  2. [技术] OIer的C++标准库 : 字符串库<string>

    引入 上次我在博客里介绍了OI中可能用到的STL中的功能, 今天我们接着来发掘C++标准库中能为OI所用的部分. 众所周知, OI中经常用到字符串相关的处理, 这时善用字符串库可以使一些操作更加简洁易 ...

  3. Python Cookbook(第3版)中文版:15.14 传递Unicode字符串给C函数库

    15.14 传递Unicode字符串给C函数库¶ 问题¶ 你要写一个扩展模块,需要将一个Python字符串传递给C的某个库函数,但是这个函数不知道该怎么处理Unicode. 解决方案¶ 这里我们需要考 ...

  4. MySQL 字符串截取SUBSTRING()函数

    MySQL 字符串截取相关函数: 1.从左开始截取字符串 left(str, length) 说明:left(被截取字段,截取长度) 例: select left(content,200) as ab ...

  5. Python第五天 文件访问 for循环访问文件 while循环访问文件 字符串的startswith函数和split函数 linecache模块

    Python第五天   文件访问    for循环访问文件    while循环访问文件   字符串的startswith函数和split函数  linecache模块 目录 Pycharm使用技巧( ...

  6. Python第二天 变量 运算符与表达式 input()与raw_input()区别 字符编码 python转义符 字符串格式化 format函数字符串格式化 帮助

    Python第二天  变量  运算符与表达式  input()与raw_input()区别  字符编码  python转义符  字符串格式化  format函数字符串格式化  帮助 目录 Pychar ...

  7. [技术] OIer的C++标准库 : 字符串库

    引入 上次我在博客里介绍了OI中可能用到的STL中的功能, 今天我们接着来发掘C++标准库中能为OI所用的部分. 点击传送至我的上一篇系列博文 众所周知, OI中经常用到字符串相关的处理, 这时善用字 ...

  8. C++ 第十课:标准c时间与日期函数

    asctime() 时间文本格式 clock() 返回自程序开始运行所经过的时间 ctime() 返回特定格式时间 difftime() 两时刻的间隔 gmtime() 返回指向当前格林威治时间的指针 ...

  9. C 和 C++ 的标准库分别有自己的 locale 操作方法,C 标准库的 locale 设定函数是 setlocale(),而 C++ 标准库有 locale 类和流对象的 imbue() 方法(gcc使用zh_CN.GBK,或者zh_CN.UTF-8,VC++使用Chinese_People's Republic of China.936或者65001.)

    转自:http://zyxhome.org/wp/cc-prog-lang/c-stdlib-setlocale-usage-note/ [在此向原文作者说声谢谢!若有读者看到文章转载时请写该转载地址 ...

随机推荐

  1. JavaScript--事件模型(转)

    在各种浏览器中存在三种事件模型:原始事件模型( original event model),DOM2事件模型,IE事件模型.其中原始的事件模型被所有浏览器所支持,而DOM2中所定义的事件模型目前被除了 ...

  2. Oracle DBA 的常用Unix参考手册(二)

    9.AIX下显示CPU数量    # lsdev -C|grep Process|wc -l10.Solaris下显示CPU数量# psrinfo -v|grep "Status of pr ...

  3. Python抓取单个网页中所有的PDF文档

    Github博文地址,此处更新可能不是很及时. 1.背景 最近发现算法以及数据结构落下了不少(其实还是大学没怎么好好学,囧rz),考虑到最近的项目结构越来越复杂了,用它来练练思路,就打算复习下数据结构 ...

  4. oracle 11g在安装过程中出现监听程序未启动或数据库服务未注册到该监听程序

    15511477451 原文 oracle 11g在安装过程中出现监听程序未启动或数据库服务未注册到该监听程序? 环境:win7 64位系统.oracle11g数据库 问题描述:在win7 64位系统 ...

  5. OutputFormat中OutputCommitter解析

    在hadoop中,由于一个Task可能由多个节点同时运行,当每个节点完成Task时,一个Task可能会出现多个结果,为了避免这种情况的出现,使用了OutPutCommitter.所以OutPutCom ...

  6. 【管理工具】Git的安装与使用

    公司与公司合并,需要学习一下git的使用.从网上找了一篇资料,完全满足需求,先赞一个. http://www.cnblogs.com/Bonker/p/3441781.html 下面记录一下自己的安装 ...

  7. windows各种程序中文显示乱码又找不到原因时

    我电脑上的各种程序,如xshell,Navicat for MySQL都不正常显示中文,该软件的编码,utf-8,gbk,gb2312来回切换好几回,没一次正常,最好解决办法如下       进入控制 ...

  8. 为Fitnesse-20140630定制RestFixture代码

    摘要:Fitnesse插件RestFixture在最新版Fitnesse输出测试结果为html文本,而非html.本博文记录RestFixture定制代码的过程. 准备开发环境 假定你已经正确安装JD ...

  9. Allegro从.brd文件中导出器件封装

    打开.brd文件,File→Export→Libraries,除了No libraries dependencies之外,所有选项都勾选上,设定好存放路径之后,Export. 注意事项: 1. 一般的 ...

  10. 微软Azure已开始支持hadoop--大数据云计算

    微软Azure已开始支持hadoop,对于有需要弹性大数据运算的公司可能是个好消息.据悉,近期微软已提供一个预览版的Azure HDInsight(Hadoop on Azure)服务,运行在Linu ...