I know how to compare two strings to see if they are exactly the same, but how can see if one is inside another?
Example:
How can I make an if that checks if word2 has word1 in it?Code:string word1 = "one"; string word2 = "oneword";
I know how to compare two strings to see if they are exactly the same, but how can see if one is inside another?
Example:
How can I make an if that checks if word2 has word1 in it?Code:string word1 = "one"; string word2 = "oneword";
You can use the find method:Code:#include <iostream> #include <string> int main() { std::string word1 = "one"; std::string word2 = "oneword"; if (word2.find(word1) != std::string::npos) std::cout << "Found it!" << std::endl; }
Thanks
But if word2 is changed to "wordone" it doesn't work anymore, so I assume it has to be at the biginning of the other string. How can I get it to work both ways?
Last edited by eam; 11-05-2003 at 01:51 AM.
Are you sure you didnt make a typo?Originally posted by eam
Thanks
But if word2 is changed to "wordone" it doesn't work anymore, so I assume it has to be at the biginning of the other string. How can I get it to work both ways?
It should still work.
I rewrote the file and now it works... I must have screwed something up last time (it was 4 am!!).
Thanks again.