Public Group

# I'm trying to find a word rather then characters using string::Find?

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

## Recommended Posts

Hi all trying to figure out how I can do this line

string &str = *it; if(it->find(".obj")) { cout << " Found Obj ! " << endl; //Draw Graphics }

but string::Find can have character matches rather then the hole word itself qouted from

http://www.cplusplus...ng/string/find/

string[color=#000000][font=verdana, arial, helvetica, sans-serif]

### must be matched in some part of the string to be considered a match.[/font][/quote] I really don't know if there is a way to hack it so it reads the hole word for it to match then just .o or b or j Has anyone got any experience with this and could give me some hints/tips/anything else. //for every occurence of .obj check if its not equal the string non value if(it->find(".obj") != std::string::npos) { //then output to cmd that its found obj cout << "Found .Obj" << endl; }  figured it out

##### Share on other sites
I was looking at the before I shall look at it again and see if I can figure it out ;) but if anyone else has any other tips that would be great too just incase I screw this up

##### Share on other sites
Do you want get ".obj" or do you want to get the individual characters?

In your example you are showing that you are looking for the extension and do something based on that.

if that is all you want, you can just take the length of your filename, extract 3 (or 4 including the dot) spaces and start reading in from there and compare it. Or you can do some string magic and start reading from where the dot begins if you want a more general way of handling this.

You can also do this with string::find though. This is also not the most elegant way I guess, but it should work.

##### Share on other sites
What, exactly, are you trying to do. Are you trying to determine the file extension of a file name?

##### Share on other sites
First, std::string::find is overloaded to find whole strings (http://www.cplusplus.com/reference/string/string/find/) but returns the position of the found substring (or std::string::npos if the substring is not found), not a boolean value.

Aside from that, your code (after fixing) would accept any file containing the substring ".obj" as an object file, for example "mysubdir.obj/notanobjfile.txt". It would be better to find the rightmost "." (via rfind), extract the substring from there to the end and compare that to ".obj".

On the other hand I would personally never write code like that manually and just use boost::filesystem.

##### Share on other sites
 //for every occurence of .obj check if its not equal the string non value if(it->find(".obj") != std::string::npos) { //then output to cmd that its found obj cout << "Found .Obj" << endl; } 

figured it out

• 9
• 13
• 41
• 15
• 13