C ++字符串和strchr() [英] C++ strings and strchr()

查看:53
本文介绍了C ++字符串和strchr()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在字符串中找到一个字符的位置并将其替换为另一个

如果它实际存在,我希望该操作有效。我是

假设标准这样做的方式类似于


string s =" blah";

int i = s.find_first_of(''a''); / *返回2,我猜? * /

s.replace(i,0," e");


对吗?这是最好的方法(假设我没有做出一些愚蠢的错误)?是

有一种方法(轻松!)从C ++字符串中获取一个字符指针,这样我就可以改变那个字符,就好像


char * cp;


if((cp = strchr(s.c_str(),''a'')!= NULL)

* cp =''e'';


?如果两种方式都不好,有什么更好的方式?


(如果这是一个FAQ,我没看到它......)


-

Christopher Benson-Manica | Jumonji giri,荣誉。

ataru(at)cyberspace.org |

解决方案



< at * **@nospam.cyberspace.org>在留言新闻中写道:bi ********** @ chessie.cirr.com ...

string s =" ; blah" ;;
int i = s.find_first_of(''a''); / *返回2,我推测?* /


你需要检查看看如果找到成功(除非你确定它不会失败)。

它可能会返回npos。你也可以使用s。找到(''''')。因为你的比赛字符串是

只有一个字符他们都做同样的事情(我不确定那个人会是什么?
比另一个更快。

s.replace(i,0," e");




那是不是运。它说要替换零个字符。你想要的。

s.replace(i,1," e");

实际上,对于单个角色来说这也一样好

s [i] =''e'';


Ron Natalie< ro*@sensor.com>打破了永恒的沉默并且这样说话:

实际上,对于单个角色来说这将是同样好的
s [i] =''e'';



您可以引用字符串,就好像它们是C风格的char *一样?

-

Christopher Benson -Manica | Jumonji giri,荣誉。

ataru(at)cyberspace.org |


在*** @nospam.cyberspace.org

我想在字符串中找到一个字符的位置并将其替换为
另一个如果它确实在那里,我希望这个操作有效。我是假设标准这样做的方法就像

string s =" blah";
int i = s.find_first_of(''a''); / *返回2,我猜? * /
s.replace(i,0," e");




#include< algorithm>

replace (s.begin(),s.end(),'''',''e'');


-X


I want to find the position of a character in a string and replace it another
if it is actually there, and I''d like the operation to be efficient. I''m
assuming the "standard" way to do this is something like

string s = "blah";
int i = s.find_first_of(''a''); /* returns 2, I presume? */
s.replace(i,0,"e");

Right? Is this the best way (assuming I haven''t made some stupid error)? Is
there a way to (easily!) get a character pointer from a C++ string so I can
just change that one character, like maybe

char *cp;

if( (cp=strchr(s.c_str(), ''a'') != NULL)
*cp=''e'';

? If neither way is good, what''s a better way?

(if this is a FAQ, I didn''t see it...)

--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org |

解决方案


<at***@nospam.cyberspace.org> wrote in message news:bi**********@chessie.cirr.com...

string s = "blah"; int i = s.find_first_of(''a''); /* returns 2, I presume? */
You need to check to see if the find succeeds (unless you are sure it won''t fail).
It might return npos. You could also use s.find(''a''). Since your match string is
only a single character they both do the same thing (I''m not sure that one would
be faster than the other).
s.replace(i,0,"e");



That''s a no op. It says to replace zero chars. You want.
s.replace(i, 1, "e");
Actually, for a single character this would be just as good
s[i] = ''e'';


Ron Natalie <ro*@sensor.com> broke the eternal silence and spoke thus:

Actually, for a single character this would be just as good
s[i] = ''e'';



You can reference strings as though they were C-style char*''s?
--
Christopher Benson-Manica | Jumonji giri, for honour.
ataru(at)cyberspace.org |


at***@nospam.cyberspace.org:

I want to find the position of a character in a string and replace it another if it is actually there, and I''d like the operation to be efficient. I''m
assuming the "standard" way to do this is something like

string s = "blah";
int i = s.find_first_of(''a''); /* returns 2, I presume? */
s.replace(i,0,"e");



#include<algorithm>
replace(s.begin(),s.end(),''a'',''e'');

-X


这篇关于C ++字符串和strchr()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆