开发者

Searching for occurrence of a string using c++

I need to find an occurrence of string in an string. Is there any fin开发者_如何学运维d function in c++? For example, if I have a string example/example/example/a/a, how to get a number of occurrences of string example, which in this case is 3?


Yes, there is a function the finds content in a string. ;-)

You can find this and more by looking at std::string API:

http://www.cplusplus.com/reference/string/string/


Use the substring function.

  std::string to_search = "example/example/example/a/a";
  std::string to_find = "example";

  int count = 0;
  for (int i = 0; i < to_search.length() - to_find.length(); i++) {
    if (to_search.substr(i, to_find.length()) == to_find)
      count++;
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜