indexof

mingzaili 2010-01-05 11:18:07
在c#中有IndexOf可以对一个字符串查找.
如"abcde".IndexOf("cd")将返回2.

请问在c++中如何完成类似的功能呢?

...全文
94 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
2010-01-05
  • 打赏
  • 举报
回复
string("abcde").find("cd")
mingzaili 2010-01-05
  • 打赏
  • 举报
回复
那具体怎么写呢?
我刚刚接触到c++,一头雾水. 公司有个小程序要用c++写,有点急.

谢啦!!
macrojj 2010-01-05
  • 打赏
  • 举报
回复
find吧
find_first_of
等等
healer_kx 2010-01-05
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 freezezdj 的回复:]
string str = "abcde";
str.find("cd");

C++ 不支持 “abced”.find("cd"); 这种语法。
[/Quote]

那是因为C++编译器不会把 字符串字面量 处理为对象,而且也没有一个真正意义上的字符串对象。。。
pengzhixi 2010-01-05
  • 打赏
  • 举报
回复
string中的find函数
size_t find ( const string& str, size_t pos = 0 ) const;
size_t find ( const char* s, size_t pos, size_t n ) const;
size_t find ( const char* s, size_t pos = 0 ) const;
size_t find ( char c, size_t pos = 0 ) const;
pengzhixi 2010-01-05
  • 打赏
  • 举报
回复
string中的find函数
size_t find ( const string& str, size_t pos = 0 ) const;
size_t find ( const char* s, size_t pos, size_t n ) const;
size_t find ( const char* s, size_t pos = 0 ) const;
size_t find ( char c, size_t pos = 0 ) const;
pengzhixi 2010-01-05
  • 打赏
  • 举报
回复
string中的find函数
size_t find ( const string& str, size_t pos = 0 ) const;
size_t find ( const char* s, size_t pos, size_t n ) const;
size_t find ( const char* s, size_t pos = 0 ) const;
size_t find ( char c, size_t pos = 0 ) const;
冻结 2010-01-05
  • 打赏
  • 举报
回复
string str = "abcde";
str.find("cd");

C++ 不支持 “abced”.find("cd"); 这种语法。
pengzhixi 2010-01-05
  • 打赏
  • 举报
回复
string中的find函数
healer_kx 2010-01-05
  • 打赏
  • 举报
回复
find... ...
wanjiwz 2010-01-05
  • 打赏
  • 举报
回复
[Quote=引用楼主 mingzaili 的回复:]
在c#中有IndexOf可以对一个字符串查找.
如"abcde".IndexOf("cd")将返回2.

请问在c++中如何完成类似的功能呢?


[/Quote]
strstr()函数可以返回指向子串首地址的指针,然后可以计算出偏移量来

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧