Comparing a string to data within another string

Hello all,

I am writing a program and I need to compare one string with a larger string in order to see if there is a match of characters anywhere in the string. For example, if I have a string with the value 'asdf', I need to be able to tell that it has a match WITHIN another string with the value of 'lasdf'. Think control-f, but with strings.

My question is: is there a method in c++ or Borland VCL to do this? I know I can do a comparison of char arrays and write my own code, but it would be nice to know if there is a method that can do this for me.

Thanks in advanc
Something like this?
1
2
3
4
5
6
7
std::string haystack = "some text to search";
std::string needle = "text";

if(haystack.find(needle) != std::string::npos)
{
    // needle found in haystack...
}


http://cplusplus.com/reference/string/string/find/
I would support the use of std::string as Galik suggested. But if you are definitely not using those and want to stick to char arrays, you can use strcmp(), stricmp() or its wide character equivalents if you are using wchar_t. Google them for examples and syntax.
Thanks guys, both options appear to work great!
Topic archived. No new replies allowed.