字符串查找和替换接口
2016-02-28 09:35
253 查看
int replace_str(std::string& str, const char * oldpart, const char * newpart)
{
int
nReplaced= 0;
std::string::size_type nIdx= 0;
std::string::size_type nOldLen= strlen(oldpart);
if ( 0 == nOldLen )
return 0;
static const char ch = 0x00;
std::string::size_type nNewLen= strlen(newpart);
const char* szRealNew= newpart == 0 ? &ch : newpart;
while ( (nIdx=str.find(oldpart, nIdx)) != std::string::npos )
{
str.replace(str.begin()+nIdx, str.begin()+nIdx+nOldLen, szRealNew);
nReplaced++;
nIdx += nNewLen;
}
return nReplaced;
}
功能: 从str中查找oldpart, 并替换成newpart.
{
int
nReplaced= 0;
std::string::size_type nIdx= 0;
std::string::size_type nOldLen= strlen(oldpart);
if ( 0 == nOldLen )
return 0;
static const char ch = 0x00;
std::string::size_type nNewLen= strlen(newpart);
const char* szRealNew= newpart == 0 ? &ch : newpart;
while ( (nIdx=str.find(oldpart, nIdx)) != std::string::npos )
{
str.replace(str.begin()+nIdx, str.begin()+nIdx+nOldLen, szRealNew);
nReplaced++;
nIdx += nNewLen;
}
return nReplaced;
}
功能: 从str中查找oldpart, 并替换成newpart.
相关文章推荐
- 【leetcode】【16】3Sum Closest
- C# 命名规范
- Monkey常见面试问题
- 观感1
- iOS开发系列—Objective-C之Foundation框架
- 《基于MFC的OpenGL编程》Part 4 Transformations - Rotations, Translations and Scaling
- Android 高仿微信头像截取 打造不一样的自定义控件
- Sqoop处理Clob与Blob字段
- Ebean Demo
- Android数据存储——sqlite3数据库管理工具
- Android 高仿微信6.0主界面 带你玩转切换图标变色
- 关于苹果开发证书失效的解决方案(2016年2月14日Failed to locate or generate matching signing assets)
- 个人是时代的一朵浪花
- spring-boot 整合redis作为数据缓存
- 关于跨进程的观察者模式
- Android 自定义控件 优雅实现元素间的分割线 (支持3.0以下)
- Ubuntu 14.04 install emacs 24.5
- 安全检测JavaScript基本数据类型和内置对象
- Swift - 数组排序方法(附样例)
- 关于Cygwin中编译FFmpeg时执行sh文件出现arm-linux-androideabi-gcc is unable to create an executable file.