strstr

Which method is preferred strstr or strpos?

对着背影说爱祢 提交于 2019-11-26 13:21:39
问题 I noticed a lot of developers are using both strstr and strpos to check for a substring existence. Is one of them preferred and why ? 回答1: From the PHP online manual: If you only want to determine if a particular needle occurs within haystack, use the faster and less memory intensive function strpos() instead. 回答2: Here are some other answers (+benchmarks) I got to my question, which is almost the same (I didn't realize yours when asking). In the meantime I also made my own benchmark test,

strstr not functioning

本小妞迷上赌 提交于 2019-11-26 12:47:14
问题 Why does this particular piece of code return false on the strstr() if I input \"test\"? char input[100]; int main() { fgets(input, 100, stdin); printf(\"%s\", input); if(strstr(\"test message\", input)) { printf(\"strstr true\"); } } I thought strstr searched the first param for instances of the second param? It works when I replace input with some text or just assign it something directly, but it seems to not work with fgets. 回答1: It's because fgets stores the newline character so when