Here's a quick example. As you can see, it's a bit convoluted. #include <iostream>
#include <string.h>
using namespace std;
int main(int argc, void* argv[]) {
char* str = "abcabc<defdef";
char* match = strstr(str, "<");
char substr[1024];
// If no match, strstr points to a zero-length string
if (strlen(match) == 0)
return 0;
// match is the address of the match, str is the address of the string,
// so match - str is the length from the start of the string to the match.
int substrlength = match - str;
// Copy the first 'substrlength' chars from the original into the substring.
// Also, you need to null-terminate the string which is what the line below does.
strncpy(substr, str, (size_t)substrlength);
substr[substrlength] = 0;
cout << substr << endl;
return 0;
}It will output abcabc to the console.
Note that strstr does not return the position of the substring inside the string, it returns the memory address of the match. For example, if "abcabc<defdef" is located at memory address 0x12345670, matching < with strstr will return a char* pointing to 0x1234676, because that's where the < is located.
If you don't understand the way memory is handled in C++, you best learn before you start trying to get your head around things like strings. It'll benefit you greatly.