社区
工具平台和程序库
帖子详情
◎◎怎样用string.replace()完成字符串的替换功能呢?
ddmor
2004-01-18 08:33:42
比如我想把字符串"abcdefg"中的"def"替换为"yxz",那该怎么办呢?
我用string.replace()老是出错啊。
...全文
365
3
打赏
收藏
◎◎怎样用string.replace()完成字符串的替换功能呢?
比如我想把字符串"abcdefg"中的"def"替换为"yxz",那该怎么办呢? 我用string.replace()老是出错啊。
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
3 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
梦想成了相扑
2004-08-25
打赏
举报
回复
试试这个函数。更多精彩,尽在:http://blog.csdn.net/mxclxp/archive/2004/08/25/84818.aspx(原创!!)
//////////////////////////////////////////////////////////////////////////
/*
* 功能: 把 "原始串" 中所有的 "替换源串" 替换成 "替换目的串",返回替换完
的结果。
* 意义: 此功能解决了 std::string 中替换功能的单一,方便了编程。
*/
//////////////////////////////////////////////////////////////////////////
std::string stringReplace(const std::string& input, // 原始串
const std::string& find, // 替换源串
const std::string& replaceWith) // 替换目的串
{
std::string strOut(input);
int curPos = 0;
int pos;
while((pos = strOut.find(find, curPos)) != -1)
{
strOut.replace(pos, find.size(), replaceWith); // 一次替换
curPos = pos + replaceWith.size(); // 防止循环替换!!
}
return strOut;
}
donkey829
2004-01-19
打赏
举报
回复
agree to
merlinran
2004-01-18
打赏
举报
回复
为了替换,你必须先找到要替换的串的位置。
string s("abcdefg");
string::size_type sz = s.find("def");
// 验证确实找到了
if (sz != string::npos)
{
s.replace(sz, 3, "yxz");
}
字符串
替换
Replace
仅
替换
第一个
字符串
匹配项
C#里面的
String
.
Replace
(
string
,
string
)方法
替换
的时候是
替换
所有的匹配项,我们需要只
替换
第一个匹配项,写一个方法来实现这个
功能
String
.
replace
()
替换
字符串
中指定的字符
String
.
replace
() 在
字符串
中用一些字符
替换
另一些字符,或
替换
一个与正则表达式匹配的子串 语法:
string
Object.
replace
(regexp/substr,
replace
ment) 参数: regexp/substr 必需。规定子
字符串
或要
替换
的...
C++实现
string
.
replace
(
字符串
替换
)
#include "stdafx.h" #include #include <
string
> using namespace std;void
string
_
replace
( std::
string
&strBig, const std::
string
&strsrc, const std::
string
&strdst); int _tmain(int argc, _TCHA
C#用
string
.
Replace
方法批量
替换
某一类
字符串
返回一个新
字符串
,其中当前实例中出现的所有指定
字符串
都使用提供的区域性和区分大小写属性
替换
为另一个指定的
字符串
。返回一个新
字符串
,其中当前实例中出现的所有指定
字符串
都使用提供的比较类型
替换
为另一个指定...
[C++]
string
类的
字符串
替换
replace
()方法及扩展应用详述
C++的
string
库提供了专门的函数方法来实现
字符串
的
替换
:
string
.
replace
() 但是这个函数将源
字符串
中某个
字符串
只
替换
了一次,
string
类并没有实现对于源
字符串
中的某个
字符串
全部
替换
。 介绍: 在C++11标准中...
工具平台和程序库
24,854
社区成员
27,343
社区内容
发帖
与我相关
我的任务
工具平台和程序库
C/C++ 工具平台和程序库
复制链接
扫一扫
分享
社区描述
C/C++ 工具平台和程序库
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章