转自: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. H.264学习笔记之一(层次结构,NAL,SPS)

    一 H.264句法 1.1元素分层结构 H.264编码器输出的Bit流中,每个Bit都隶属于某个句法元素.句法元素被组织成有层次的结构,分别描述各个层次的信息. 图1 H.264分层结构由五层组成,分 ...

  2. solr4.2 solrconfig.xml配置文件简单介绍

    对于solr4.x的每个core有两个很重要的配置文件:solrconfig.xml和schema.xml,下面我们来了解solrconfig.xml配置文件. 具体很详细的内容请细读solrcofi ...

  3. 使用rsync同步Linux数据到Windows

    windows: win7,cwrsyncserver 4.1.0 linux:ubuntu 14.04,rsync 3.1.0 networks:使用360wifi [Windows端] 是否使用管 ...

  4. context.Response.End()的用法和本质

    using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Web_C ...

  5. winfrom dataGridView 自定义分页实现

    Winfrom 基本处于忘光的阶段.先需要做个winfrom 的软件.然后自己扩展了DataGridView带分页的控件.废话不多说 上图先   现在一步步实现其效果. 1.添加用户控件 上图即可知道 ...

  6. delete drop truncate

    一.相同点 1 truncate.不带where子句的delete.drop都会删除表内的数据2 drop.truncate都是DDL语句(数据定义语言),执行后会自动提交 二.不同点 1trunca ...

  7. MyBatis一对多双向关联——MyBatis学习笔记之七

    处理has-one关系需要用到association元素,而处理has many关系则需要用到collection元素.例如本例中,假设一 名教师可同时指导多名学生,下面就来介绍如何使用collect ...

  8. Delphi TRichEdit加载word内容

    procedure TForm1.btn6Click(Sender: TObject);var WordApp: Variant; //声明一个word对象beginWordApp := Create ...

  9. Ubuntu 14.04配置FTP服务器

    搭建: 1.sudo apt-get update                                        #更新软件 2.sudo apt-get install vsftpd ...

  10. XSLT 调用java

    XSLT调用JS  http://www.ibm.com/developerworks/cn/xml/tips/x-tipxsltjs/index.htmlXSLT调用JAVA  http://unm ...