Sign in to follow this  
DominicHughes

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

Recommended Posts

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

[code]string &str = *it;

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

cout << " Found Obj ! :D" << endl;

//Draw Graphics

}[/code]

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

[url="http://www.cplusplus.com/reference/string/string/find/"]http://www.cplusplus...ng/string/find/[/url]

[quote][url="http://www.cplusplus.com/string"]string[/url][color=#000000][font=verdana, arial, helvetica, sans-serif][size=3] to be searched for in the object. The entire content of [/size][/font][/color][i]str[/i][color=#000000][font=verdana, arial, helvetica, sans-serif][size=3] must be matched in some part of the string to be considered a match.[/size][/font][/color][/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.


[CODE]
//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;
}
[/CODE]

figured it out [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Share this post


Link to post
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 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 ([url="http://www.cplusplus.com/reference/string/string/find/"]http://www.cplusplus.com/reference/string/string/find/[/url]) 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
[CODE]
//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;
}
[/CODE]

figured it out [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this