Index of the nth occurrence of a row

How to find the index of the nth occurrence of a given string in a string? I need this to take a substring from this index. Is this possible with any functions in C ++?

+4
source share
6 answers

Boost has a find_nth template find_nth : http://www.boost.org/doc/libs/1_54_0/doc/html/boost/algorithm/find_nth.html

 #include <iostream> #include <boost/algorithm/string/find.hpp> using namespace std; using namespace boost; int main() { string a = "The rain in Spain falls mainly on the plain"; iterator_range<string::iterator> r = find_nth(a, "ain", 2); cout << distance(a.begin(), r.begin()) << endl; return 0; } 
+9
source

You can use the following function

 #include <string.h> int strpos(char *haystack, char *needle, int nth) { char *res = haystack; for(int i = 1; i <= nth; i++) { res = strstr(res, needle); if (!res) return -1; else if(i != nth) res = res++; } return res - haystack; } 

Returns -1 if it cannot find the nth occurrence.

+5
source

An easy way to do this using only std :: string :: find

 size_t find_nth(const string& haystack, size_t pos, const string& needle, size_t nth) { size_t found_pos = haystack.find(needle, pos); if(0 == nth || string::npos == found_pos) return found_pos; return find_nth(haystack, found_pos+1, needle, nth-1); } 
+3
source

this template function should complete the task

 template<typename Iter> Iter nth_occurence(Iter first, Iter last, Iter first_, Iter last_, unsigned nth) { Iter it = std::search(first, last, first_, last_); if (nth == 0) return it; if (it == last) return it; return nth_occurence(it + std::distance(first_, last_), last, first_, last_, nth -1); } 

using

 int main() { std::string a = "hello world world world end"; std::string b = "world"; auto it1 = nth_occurence(begin(a), end(a), begin(b), end(b), 0); auto it2 = nth_occurence(begin(a), end(a), begin(b), end(b), 1); auto it3 = nth_occurence(begin(a), end(a), begin(b), end(b), 2); auto it4 = nth_occurence(begin(a), end(a), begin(b), end(b), 3); std::cout << std::distance(begin(a), it1) << "\n"; std::cout << std::distance(begin(a), it2) << "\n"; std::cout << std::distance(begin(a), it3) << "\n"; std::cout << std::boolalpha << (it4 == end(a)) << "\n"; } => 6, 12, 18, true 
+1
source

To do this, you can use std::string::find and track the returned position. At the same time, you can check if the desired string is not found and return -1.

 #include <string> int nthOccurrence(const std::string& str, const std::string& findMe, int nth) { size_t pos = 0; int cnt = 0; while( cnt != nth ) { pos+=1; pos = str.find(findMe, pos); if ( pos == std::string::npos ) return -1; cnt++; } return pos; } 
+1
source

I really like the rcs answer that made smart use of pointers. I think that besides using the boost library, this is the cleanest way to achieve the result that the OP wants. However, I was not able to implement it in certain codes that did not use pointers (I'm still a beginner), so here is an equivalent answer that uses neither pointers nor boost library.

 int strpos(string haystack, char needle, int nth) {// Will return position of n-th occurence of a char in a string. string read; // A string that will contain the read part of the haystack for (int i=1 ; i<nth+1 ; ++i) { std::size_t found = haystack.find(needle); read += haystack.substr(0,found+1); // the read part of the haystack is stocked in the read string haystack.erase(0, found+1); // remove the read part of the haystack up to the i-th needle if (i == nth) { return read.size(); } } return -1; } 
0
source

All Articles