tags:

views:

123

answers:

3

if you know the start and end positions in string from where to begin and end the search. For example -

string s = StringStringString

|S |t |r |i |n |g |S |t |r |i |n |g |S |t |r |i |n |g
 0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17

How would you find "tr" in the string specifying the the position to begin search is at index 6 and the position to end the search is index 9.

I'm trying a set a limit of search so it wouldn't go beyond it.

+1  A: 

Yes, you can do that with string::find.


std::string str("StringStringString");
std::string::size_type found = str.find("tr", 6, 9 - 6);
Bertrand Marron
post some code, please
anon
Yeah, I edited, thank you for the advice – if it was one.
Bertrand Marron
The return from find is not size_t, and you've failed to account for the possibility that the substring isn't found.
Jerry Coffin
The OP is asking for a way to find a string in another one. It's up to him to read the documentation. – It makes more sense to me to return a non-signed value for a size or a position. But you're right, it returns `std::string::size_type`.
Bertrand Marron
Ok, first I thought the code would work but it didn't when i actually tried. the third parameter here is the length of characters to search for, it doesn't provide a limit of search.
Dave18
@Jerry: I think that assigning the result of `string::find` to a `size_t` is safe almost always. `size_t` is a type large enough for the size of any object, and it's generally considered safe (although not actually guaranteed) to assume that string holds the char data in a single array. Hence the size of a string cannot exceed the limit on the size of an object. A more pressing problem with this solution is that the third param to `find` isn't the length of the region of the string to search, it's the length of the string being searched for. This code is looking for the sequence t, r, NUL.
Steve Jessop
A: 

Given start as the position where you want to start searching, and stop as the position where you want to stop searching:

int pos = your_string.find("tr", start);

if (pos == std::string::npos || pos > stop)
    // it wasn't found.

This assumes that what you're giving as stop is that last position at which a match can begin. So (for example) if you want to match a substring that's four characters long, it can extend up to three characters past the stop position. If you want to assure the end of the substring is before the stop point, you'd subtract the length of the substring from stop when you compare to pos

Jerry Coffin
what is "stop"? and "start"?
anon
I wasn't thinking about the if statement but rather finding a function to do that :S
Dave18
what is "npos"? std::string::npos? – std::string::find returns a size_t.
Bertrand Marron
@tusbar:actually, it returns a `size_type`, which may or may not be the same as `size_t`. According to §21.3.6.1/2, it returns npos if the substring wasn't found.
Jerry Coffin
maybe i have also been misinformed about using size_t as return type at the example on http://www.cplusplus.com/reference/string/string/find/ where can i find 21.3.6.1/2?
Dave18
@Dave17: That's just some ultra-pedantry. `string::size_type` comes from `Allocator::size_type`, and default allocator's `size_type` is `size_t`. Now it could be possible that you have a string with a different allocator and different size type, but `std::string` by definition is `std::basic_string<char, std::char_traits<char>, std::allocator<char> >`
UncleBens
@Dave17:In the C++ standard.
Jerry Coffin
+3  A: 

If you really want to limit the length of the sequence that gets traversed (presumably because the string is very long compared to the interesting region), use std::search and pass it the corresponding iterators into the string.

avakar