C语言 在 C 中检查一个字符串是否包含另一个字符串的简单方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15098936/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple way to check if a string contains another string in C?
提问by iaacp
I'm pretty new to the language. Let's say I have a string from an HTTP request, such as
我对这门语言很陌生。假设我有一个来自 HTTP 请求的字符串,例如
char * request = "GET /favicon.ico HTTP/1.1";
And I specifically want to know if faviconis in that request, perhaps with a boolean value. What is a relatively simple way to go about this? I know how to do it in Java, but I'm more lost with C.
我特别想知道是否favicon在该请求中,可能带有布尔值。有什么相对简单的方法来解决这个问题?我知道如何用 Java 做到这一点,但我更迷失在 C 中。
Thanks!
谢谢!
回答by
if (strstr(request, "favicon") != NULL) {
// contains
}
回答by Fred Foo
strstr(request, "favicon") != NULL

