Jump to content
  • Advertisement
Sign in to follow this  
GT70sgt

C++ String Search

This topic is 5145 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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...

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
no, but google can

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
yeah....it didnt work

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
yeah....it didnt work


"it didn't work" is not a valid problem description.

Share this post


Link to post
Share on other sites
You're going to need to be a bit more clear about exactly what is going on if you want any help.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!