转自: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. I.MX6 android 获取framebuffer信息

    /******************************************************************************** * I.MX6 android 获取 ...

  2. Java [Leetcode 83]Remove Duplicates from Sorted List

    题目描述: Given a sorted linked list, delete all duplicates such that each element appear only once. For ...

  3. Android精美的日历控件

    网上看到的精美日历控件,谨以此文记录一下,用到的时候再来翻翻 源码地址 : http://download.csdn.net/detail/abc13939746593/7265459

  4. [shell]通过ping检测整个网段IP的网络状态脚本

    要实现Ping一个网段的所有IP,并检测网络连接状态是否正常,很多方法都可以实现,下面简单介绍两种,如下:脚本1#!/bin/sh# Ping网段所有IP# 2012/02/05ip=1 #通过修改初 ...

  5. hdu 2059(dp)

    题意:容易理解... 思路:dp[i]表示乌龟到达第i个充电站时最少花费时间到第 i 个充电站后,从起点开始遍历到第 i-1 个充电站,得到最少花费时间 状态转移方程:dp[i]=min(dp[j]+ ...

  6. codeforces 687D Dividing Kingdom II 带权并查集(dsu)

    题意:给你m条边,每条边有一个权值,每次询问只保留编号l到r的边,让你把这个图分成两部分 一个方案的耗费是当前符合条件的边的最大权值(符合条件的边指两段点都在一个部分),问你如何分,可以让耗费最小 分 ...

  7. HDU-1584 蜘蛛牌(dfs)

    可以多看看. 蜘蛛牌 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  8. 【LeetCode 229】Majority Element II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  9. 用javascript 面向对象制作坦克大战(三)

    之前,我们完成了坦克的移动和地图的绘制,这次我们来完成碰撞检测和炮弹的发射. 上代码前来张最新的类图: 3. 碰撞检测     前面我们已经完成了坦克的移动和地图的绘制,下面我们开始写碰撞检测. 3. ...

  10. 分享一个Web弹框类

    using System; using System.Text; namespace Core { /// <summary> /// MessageBox 的摘要说明. /// < ...