C++ String Search

Started by
14 comments, last by GameDev.net 19 years, 7 months ago
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...
C++ ProgrammerAIM - GT70sgtLead Guitar in Distorted Play
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
can you show me a working example of what you mean by that
thanks.
C++ ProgrammerAIM - GT70sgtLead Guitar in Distorted Play
no, but google can
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.
no thats ok, i want it to be case sensitive...lol

yea so...how would that work in borland??
C++ ProgrammerAIM - GT70sgtLead Guitar in Distorted Play
Exactly the same. Don't use the borland libraries. Use the standard library.
yeah....it didnt work
Quote:Original post by Anonymous Poster
yeah....it didnt work


"it didn't work" is not a valid problem description.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
You're going to need to be a bit more clear about exactly what is going on if you want any help.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

This topic is closed to new replies.

Advertisement