标准C++ 字符串处理增强函数
转自: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++ 字符串处理增强函数的更多相关文章
- Python标准库:内置函数hasattr(object, name)
Python标准库:内置函数hasattr(object, name) 本函数是用来判断对象object的属性(name表示)是否存在.如果属性(name表示)存在,则返回True,否则返回False ...
- [技术] OIer的C++标准库 : 字符串库<string>
引入 上次我在博客里介绍了OI中可能用到的STL中的功能, 今天我们接着来发掘C++标准库中能为OI所用的部分. 众所周知, OI中经常用到字符串相关的处理, 这时善用字符串库可以使一些操作更加简洁易 ...
- Python Cookbook(第3版)中文版:15.14 传递Unicode字符串给C函数库
15.14 传递Unicode字符串给C函数库¶ 问题¶ 你要写一个扩展模块,需要将一个Python字符串传递给C的某个库函数,但是这个函数不知道该怎么处理Unicode. 解决方案¶ 这里我们需要考 ...
- MySQL 字符串截取SUBSTRING()函数
MySQL 字符串截取相关函数: 1.从左开始截取字符串 left(str, length) 说明:left(被截取字段,截取长度) 例: select left(content,200) as ab ...
- Python第五天 文件访问 for循环访问文件 while循环访问文件 字符串的startswith函数和split函数 linecache模块
Python第五天 文件访问 for循环访问文件 while循环访问文件 字符串的startswith函数和split函数 linecache模块 目录 Pycharm使用技巧( ...
- Python第二天 变量 运算符与表达式 input()与raw_input()区别 字符编码 python转义符 字符串格式化 format函数字符串格式化 帮助
Python第二天 变量 运算符与表达式 input()与raw_input()区别 字符编码 python转义符 字符串格式化 format函数字符串格式化 帮助 目录 Pychar ...
- [技术] OIer的C++标准库 : 字符串库
引入 上次我在博客里介绍了OI中可能用到的STL中的功能, 今天我们接着来发掘C++标准库中能为OI所用的部分. 点击传送至我的上一篇系列博文 众所周知, OI中经常用到字符串相关的处理, 这时善用字 ...
- C++ 第十课:标准c时间与日期函数
asctime() 时间文本格式 clock() 返回自程序开始运行所经过的时间 ctime() 返回特定格式时间 difftime() 两时刻的间隔 gmtime() 返回指向当前格林威治时间的指针 ...
- 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/ [在此向原文作者说声谢谢!若有读者看到文章转载时请写该转载地址 ...
随机推荐
- 一个响应式框架——agera
Google在上周开源了一个响应式框架——agera,相信它会慢慢地被广大程序员所熟知.我个人对这样的技术是很感兴趣的,在这之前也研究过RxJava,所以在得知Google开源了这样的框架之后第一时间 ...
- Selenium2Library系列 keywords 之 _SelectElementKeywords 之 get_selected_list_values(self, locator)
def get_selected_list_values(self, locator): """Returns the values of selected elemen ...
- c#基类 常用数据验证的封装,数字,字符,邮箱的验证
#region 常用数据验证的封装,数字字符的验证 /// <summary> /// 常用数据验证的封装,数字字符的验证 /// </summa ...
- 产品设计敏感度之kindle touch 4
这种训练方法来自于<嵌入式系统开发之道:菜鸟成长日志与项目经理的私房菜>,名字真够长的,但是里面都是干货,我虽然之前有短评这本书,但是后面看完之后会继续再做一个详细的感悟记录. 选出身边的 ...
- [Hive - LanguageManual] Import/Export
LanguageManual ImportExport Skip to end of metadata Added by Carl Steinbach, last edited by Le ...
- EasyMock
使用 EasyMock 更轻松地进行测试 窥探EasyMock(1)基础使用篇 窥探EasyMock(2)进阶使用篇
- Apache Maven 入门
Apache Maven 入门篇 ( 上 ) Apache Maven 入门篇 ( 下 ) ~$mvn archetype:generate -DgroupId=com.mycompany.hello ...
- HDU4869:Turn the pokers(快速幂求逆元+组合数)
题意: 给出n次翻转和m张牌,牌相同且一开始背面向上,输入n个数xi,表示xi张牌翻转,问最后得到的牌的情况的总数. 思路: 首先我们可以假设一开始牌背面状态为0,正面则为1,最后即是求ΣC(m,k) ...
- 小C的填数游戏
题意: 给出一张n个点的无向图 i连向i-1和i-2 边权为wij 有两个点权ai和bi ai为0或1 在给m个操作 1.将ai异或1 2.将区间x到y的点都填上一个数ci 使得Σ(bi*(ai^ci ...
- javap 可以打印出用于jni调用的java函数的签名信息
javap可以打印出java的字节码: -c Prints out disassembled code, i.e., the instructions that comprise the Ja ...