String Find issue.

Feb 9, 2016 at 7:42pm
Hi Everyone!

I'm having a little problem with the std::string find function. It's not working in the applied section, so I made a little test run and it's still not working. Here's the code:

1
2
3
4
5
6
7
std::cout << "Testing Find Function\n";
	string test = "Miner_Iron";
	if (test.find("Miner")) {
		std::cout << "Found\n";
	} else {
		std::cout << "NOT found\n";
	}


The result I am getting is "NOT found", and I can't figure why, since "Miner" is clearly present in the string. It DOES find it if the search string is "Miner_Iron" however.

Also, if I set the test string to "Miner Iron" (without the underscore), then search for "Miner", is also comes up "NOT found".

Could someone explain why the string isn't being found and how to get around it please?

EDIT - Ok, I've found that if I set the test string to "AMiner_Iron", it will be found. But why does the find function ignore the first character in a string? It doesn't really make sense, and it's quite inconvenient.

Thanks,
Hibblejaybob
Last edited on Feb 9, 2016 at 8:01pm
Feb 9, 2016 at 7:59pm
The result I am getting is "NOT found", and I can't figure why,

Perhaps you need to review the documentation for std::string.find(). This function returns the location within the string where the search string is found. In this case it is returning zero because the location is the first character. Remember zero is equal to false. Also note that if the search fails the function returns std::string::npos a very large unsigned type. You should be testing against this value, not for true or false.

Something like:
1
2
3
4
5
6
7
    std::cout << "Testing Find Function\n";
	string test = "Miner_Iron";
	if (!(test.find("Miner") == std::string::npos)) {
		std::cout << "Found\n";
	} else {
		std::cout << "NOT found\n";
	}
Last edited on Feb 9, 2016 at 7:59pm
Feb 9, 2016 at 8:22pm
Ah thanks jlb, it seems I have been very confused about how the find function works.

Thanks for letting me know, I'll get the code changed!
Feb 9, 2016 at 9:30pm
[Edit] Removed - I got it working (schoolboy error)
Last edited on Feb 9, 2016 at 9:34pm
Feb 9, 2016 at 9:32pm
Perhaps you would like to post your code? I mean, did you think not posting it would make us be able to help you out better?
Last edited on Feb 9, 2016 at 9:32pm
Feb 9, 2016 at 9:41pm
... I don't understand what you mean TarikNeaj: the code was posted when I made this post. Perhaps you can simply scroll up and look before you start being rude!
Feb 9, 2016 at 9:45pm
He is probably referring to that "Removed" post. Which is why it's a good idea not to remove content from an existing post.

Feb 9, 2016 at 9:51pm
Fair enough, but there is a way of asking a question without being a prick about it, and that isn't it.

I massively appreciate anyone who will take the time to try and help me and others out, but not if they're going to be like that.....
Feb 9, 2016 at 9:55pm
I was genuinely curious, that is why I asked, it wasn't meant as an insult, sorry if you took it that way. Also, when I wrote that post you had no code, so let's not throw accusations around =)
Last edited on Feb 9, 2016 at 9:55pm
Feb 9, 2016 at 10:02pm
Ah, it's alright. I'm sorry too!
Topic archived. No new replies allowed.