• Advertisement
Sign in to follow this  

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

This topic is 2120 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

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

string &str = *it;

if(it->find(".obj"))
{

cout << " Found Obj ! :D" << 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]

to be searched for in the object. The entire content of [/font]str[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 smile.png

Share this post


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


Link to post
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 this post


Link to post
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 this post


Link to post
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 smile.png

Share this post


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

  • Advertisement