簡體   English   中英

在 C 中使用 INSIDE function

[英]Using the INSIDE function in C

小查詢:在 Python 中,我知道您可以通過以下方式檢查 char a是否在數組b內:

 b = ['a', 'b', 'c', ...] # would be filled with letters
 if str(a) in b:
      # instructions.

語言 C 中是否有類似的運算符或方法?

如果針是單個char ,那么您可以使用strchrmemchr strchr僅在 haystack 被 null 終止時才有效,但您不需要提前知道長度。 即使沒有 null 終結器, memchr也可以工作,但您確實需要告訴它長度。 每個例子:

#include <string.h>

char b1[] = { 'a', 'b', 'c' /* ... */, '\0' };
if(strchr(b1, a)) /* ... */;

char b2[] = { 'a', 'b', 'c' /* ... */ };
if(memchr(b2, a, sizeof b2)) /* ... */;

如果 needle 是一個char數組,那么您可以使用strstrmemmem ,但有類似的區別。 每個例子:

#define _GNU_SOURCE
#include <string.h>

char b1[] = { 'a', 'b', 'c' /* ... */, '\0' };
if(strstr(b1, a)) /* ... */;

char b2[] = { 'a', 'b', 'c' /* ... */ };
if(memmem(b2, sizeof b2, a, sizeof a)) /* ... */;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM