C++ String Search
Ok, here we go.
I have borland c++ 6...and im wondering how to search through all parts of a string.
for example you have these
AnsiString names[2] = {"Save A Horse Ride A Cowboy", "Rednecks"};
haha, making fun of rednecks...
anyways, i would like to know a way to have another variable, maybe ansistring that the user gives you, and then have that as terms to search through the names. So for example if i type "Ride", it would give me the first song and if i type "necks" it would give me the second song. I would like it to search through the whole string to see if what the user of my program typed in is in any part of that string.
could anyone help me please...
Use std::string::find().
a.find(b), where a is a std::string and b is a character, C-string or std::string will return std::string::npos if b isn't a sub-string of a, and will return the offset at which b is found in a otherwise.
a.find(b), where a is a std::string and b is a character, C-string or std::string will return std::string::npos if b isn't a sub-string of a, and will return the offset at which b is found in a otherwise.
std::string song = "Save a Horse Ride a Cowboy";std::string search = "Ride";if (song.find(search) != std::string::npos) cout << "Found search inside song!";
Of course, if search was "ride" instead, it wouldn't work. Best bet is to convert everything to uppercase, and then convert the input to uppercase too.
Quote:Original post by Anonymous Poster
yeah....it didnt work
"it didn't work" is not a valid problem description.
You're going to need to be a bit more clear about exactly what is going on if you want any help.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement