回答
您可以使用
if (!strncmp("GET ", str, 4)
{
...
}
else if (!strncmp("POST ", str, 5))
{
...
}
else
{
...
}
当然他需要strncmp。 strcmp(“GET”,“GET LOST”)不会返回0. – bmargulies 2010-05-07 18:04:53
使用STRNCMP与参数字符串1,字符串,n,其中字符串1和字符串是字符串进行比较和n个字符数。如果字符串匹配,则strncmp返回0或<如果string1按字典顺序小于string2,则返回0;如果string2小于string1,则返回> 0。例子:
#include <string.h>
...
strncmp(somestring, "GET ", 4) == 0
strncmp(somestring, "POST ", 5) == 0
#include <stdio.h>
#include <string.h>
typedef enum httpmethod
{
HTTP_ERR,
HTTP_GET,
HTTP_POST,
HTTP_METHOD_COUNT
} httpmethod;
const char* http_method_str[HTTP_METHOD_COUNT + 1] =
{
"UNKNOWN",
"GET ",
"POST ",
0
};
httpmethod str_get_http_method(const char* str)
{
if (!str || strlen(str) < 4)
return HTTP_ERR;
const char* ptr[HTTP_METHOD_COUNT];
int i;
int failcount = 0;
for (i = 1; i < HTTP_METHOD_COUNT; ++i)
ptr[i] = http_method_str[i];
while (*str != '\0' && failcount < HTTP_METHOD_COUNT - 1)
{
for (i = 1; i < HTTP_METHOD_COUNT; ++i)
{
if (ptr[i] && *str != *ptr[i]++)
{
ptr[i] = 0;
++failcount;
}
}
str++;
}
for (i = 1; i < HTTP_METHOD_COUNT; ++i)
if (ptr[i])
return i;
return HTTP_ERR;
}
int main(int argc, char** argv)
{
const char* test[4] = { "GET ", "POST ", "GIT ", "PAST " };
httpmethod result = HTTP_ERR;
int i;
for (i = 0; i < 4; ++i)
{
printf("checking str: %s\n", test[i]);
result = str_get_http_method(test[i]);
printf("result is type: %s\n", http_method_str[result]);
}
return 0;
}
您可能需要在初始化'http_method_str []'结尾时使用NULL。 – 2010-05-07 20:11:59
@Tim:那么枚举告诉你多少,并添加一个NULL需要一个进一步的数组索引,但我认为这样做不会有什么坏处:-) – 2010-05-07 20:42:33
你不有使用strncmp()
时,你只需要几个字符串之间进行区分:
#include <stdio.h>
#include <stdint.h>
#include <string.h>
static uint32_t method_hash(const char *key)
{
int len;
uint32_t hash;
int i;
len = strlen(key);
for (hash = 0, i = 0; i < len; i++) {
hash += (unsigned int) key[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
int main(int argc, char *argv[])
{
if (argc < 2) {
printf("Usage: %s <method>\n", argv[0]);
return 0;
}
switch(method_hash(argv[1])) {
case 802187597:
printf("Its GET\n");
break;
case 740659500:
printf("Its POST\n");
break;
default:
printf("Its RUBBISH\n");
return 1;
}
return 0;
}
只要注意,哈希是不冲突的证据,但适合了解GET和POST的区别。我经常在字典中使用那个小宝石,只有当我认为我只有一个电话strncmp()
。
我张贴这个答案告诉你,有很多种方法来处理字符串,在希望你避免代码看起来像这样,你继续学习C:
if (! strncmp(string, "FOO ", 4)) {
do_this();
} else if (! strncmp(string, "BAR ", 4)) {
do_that();
} else if (! strncmp(string, "FOOBAR ", 7)) {
do_both();
/* ... madness ensues through 200 more lines and 100 more else if's ... */
} else {
return 0;
}
我的例子是不完全正确。如果希望代码是可移植的,您希望在运行时确定散列值,而不是仅仅插入已知值。这是读者的练习(提示,开关箱需要常量)。
好的回答.... – 2010-05-07 20:46:10
但你有没有注意到空间? – 2010-05-07 20:48:52
只是在你的例子中用一大串'else if(!strncmp(string ...'语句)来评论,在这种情况下,我认为一个字符串和函数指针表是一个更清晰的解决方案。一个'str_begins()'类型的函数不需要长度参数 – tomlogic 2010-05-07 23:38:38
- 1. Python - 检查一个字符串是以“是”还是“否”开头?
- 2. 如何检查一个字符串是以一个子字符串开头还是不在java中?
- 3. 检查字符串是否以另一个字符串开头?
- 4. 如何检查字符串是否以C中的某个字符串开头?
- 5. 检查一个字符串是否以字符'23'开头 - Regex
- 6. 检查一个字符串是否以另一个字符串开头
- 7. 如何检查一个字符串是否以C中的另一个字符串开头?
- 8. 检查一个字符串是否以http开头使用Javascript
- 9. 检查一个字符串是否以XXXX开头
- 10. 检查一个字符串是否以列表中的任何字符开头
- 11. 检查字符串是否以字符串开头
- 12. KSH检查字符串是否以子字符串开头
- 13. std :: string比较(检查字符串是否以另一个字符串开头)
- 14. 使用“^”来检查字符串是否以另一个字符串开头?
- 15. 如何轻松检查一个字符串是否以4N空格开头?
- 16. 如何检查输入是整数,字符串还是float C++
- 17. 如何检查Wicket中的请求是GET还是POST?
- 18. 如何检查某个字符串是否在字符串的开头?
- 19. 如何检查一个ruby字符串是一个实际的字符串还是一个blob数据如image
- 20. 检查字符串是否以Ruby中的X字母开头
- 21. PHP检查一个字符串开头#
- 22. 检查一个字符串是否以换行符后的整数开头
- 23. 检查一个字符串是X还是不是X
- 24. 检查一个字符串是空的还是空的
- 25. 如何在codeigniter中检查请求是POST还是GET请求?
- 26. 检查varible是字符串还是int
- 27. 检查字符串以数字开头
- 28. Oracle pl/sql检查数字是以9还是8开头
- 29. 检查字符串是否以欧元/英镑符号开头
- 30. 检查字符串是否以小写字母开头
真的吗?在Google中键入“c字符串函数”工作太多了? – 2010-05-07 17:33:33