location of known substring between 2 points in a string

Probably simple, but seems I'm simpler.

I want to find location of known substring between 2 points in a string.

I think I know how to do it in steps, but wonder if it's already a thing.
yes, its built into the language, try find() https://www.cplusplus.com/reference/string/string/find/
not quite getting it, this is closest example

found = str.find("User1=", startPos, endPos);

how do I specify endPos

IDK where you found that example — it is not standard C++ and is not supported by std::string.

Are you looking for a function that finds a substring that:
  • begins in a specific subrange, but may end outside that range, or
  • both begins and ends in a specific subrange
?
you may have to use substr() with it.
I don't see a working way to specify end pos

1
2
3
4
5
6
7
8
9
10
11
int main ()
{
  std::string str ("There are two needles in this haystack with needles.");
  auto sub = str.substr(0,5);
  cout << sub << endl;
  cout << sub.find("two",3) << endl;
  sub = str.substr(5,20);
  cout << sub.find("two",3)+5 << endl; //don't forget to add the startpos back to the loc in the substring
  
 }



There
18446744073709551615
10
Last edited on
Sounds like OP wants std::search or the inconsistently-named std::find_end.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <algorithm>
#include <iostream>
#include <string_view>

int main()
{
  std::string_view const corpus = "the quick brown fox jumps over the lazy dog";
  std::string_view const target = "brown";
  
  auto const result = std::search(corpus.begin(), corpus.end(), target.begin(), target.end());
  
  if (result != corpus.end()) 
  {
    std::cout << "found \"" << target << "\" at position " << std::distance(corpus.begin(), result) << ".\n";    
  } 
}
Last edited on
thanks guys, I think this won't be one I forget, substr was my best guess but sometimes you wonder "am I doing this the hard way?"
The problem with std::string::substr() is that it creates a copy. Using the std::search() algorithm is right; you just have to code it yourself:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <algorithm>
#include <iostream>
#include <string>

std::string::size_type find_in_range( 
  const std::string    & s,      // string to search
  std::string::size_type begin,  // left bound in s (inclusive)
  std::string::size_type end,    // right bound in s (exclusive)
  const std::string    & ss  )   // substring to find
{
  auto index = std::search( s.begin()+begin, s.begin()+end, ss.begin(), ss.end() ) - s.begin();
  return (index == end) ? std::string::npos : index;
}

int main()
{
  std::string s = "A quick brown fox jumps over the lazy dog.";
  //                        [              ↑  )
  //                        9             24  27
  
  std::cout << (int)find_in_range( s, 9, 27, "over" ) << "\n";  // -1 (not found)
  std::cout << (int)find_in_range( s, 9, 28, "over" ) << "\n";  // 24 (found)
}

Hope this helps.
Topic archived. No new replies allowed.