size_t strspn(const char *str1, const char *str2) 傳回str1包含了str2 字串中的字元,共有有多少個。
char *strpbrk(const char *str1, const char *str2) 傳回str1中出現str2字串中字元的位置。若沒有找到傳回NULL.
char *d =strpbrk("my phone number is 23167736", "0123456789")
print("%c",d); ==> 卬出2
========================
以處理HTTP header 的字串為例, 我們要逐行取出HTTP header的內容, 但要如何逐行進行呢?
以下是使用 strspn(), strpbrk() 函式來完成的
參考:
[1]程式參考
#include <stdio.h>
#include <string.h>
int main ()
{
char method_str[]="GET /doc/test.html HTTP/1.1 \r\n";
char *url;
char *protocol;
char *end;
url=strpbrk(method_str," \t\n\r") ;
printf("%s\n",url);
*url++='\0';
url += strspn(url, " \t\n\r");
protocol = strpbrk(url, " \t\n\r");
*protocol++='\0';
protocol += strspn(protocol, " \t\n\r");
end = strpbrk(protocol, " \t\n\r");
*end++='\0';
printf("%s\n",method_str);
printf("%s\n",url);
printf("%s\n",protocol);
return 0;
}
沒有留言 :
張貼留言