I’m trying to compare a char * to a to a std::string
const char *s = "0@s072116\tblah\tblah\blah";
std::string id = "072116";
I need to compare these two, basically before the first \t and after the first 3 chars on the left. The width of the id can vary. 🙁
I’m not very good at C++. Any ideas??
Thanks
You can do it as follows:
If
idis a substring inss, thenfoundwill be the position of the first occurrence ofidinss.If
idis not a substring inss, thenfoundwill be a negative number.More examples on
find.Caveat:
The code above is based on the assumption your meant “…basically before the first \t and after the first 3 chars on the left…” as a way to point out where the substring would be matched in this particular example.
If instead it is a requirement that must be met for all instances (i.e.
const char *s = "0@s\tblah\tblah\blah072116"should not be matched), then the provided code sample is not sufficient.