如何检查C字符串中是否存在单个字符?

时间:2021-11-22 21:44:35

I want to check if a single char is in a C string. The character is the '|' used for pipelines in Linux (Actually, I also want to check for '<', '>', '>>', '&').

我想检查一个字符是否在C字符串中。字符是用于Linux中的管道的'|'(实际上,我还想检查'<'、'>'、'>>'、'&')。

In Java I can do this:

在Java中,我可以这样做:

String.indexOf()

But how can I do this in C, without looping through the whole string (a char* string)?

但是我如何在C中做到这一点,而不需要遍历整个字符串(char* string)?

2 个解决方案

#1


11  

If you need to search for a character you can use the strchr function, like this:

如果需要搜索字符,可以使用strchr函数,如下所示:

char* pPosition = strchr(pText, '|');

pPosition will be NULL if the given character has not been found. For example:

如果没有找到给定的字符,pPosition将为空。例如:

puts(strchr("field1|field2", '|'));

Will output: "|field2". Note that strchr will perform a forward search, to search backward you can use the strrchr. Now imagine (just to provide an example) that you have a string like this: "variable:value|condition". You can extract the value field with:

将输出:“| field2”。注意,strchr将执行向前搜索,要向后搜索,可以使用strrchr。现在假设(只是提供一个示例)您有一个这样的字符串:“variable:value|条件”。可以用以下方法提取值字段:

char* pValue = strrchr(strchr(pExpression, '|'), ':') + 1;

If what you want is the index of the character inside the string take a look to this post here on SO. You may need something like IndexOfAny() too, here another post on SO that uses strnspn for this.

如果你想要的是字符串内字符的索引,请查看这里的这个帖子。您可能还需要一些类似IndexOfAny()的内容,这里有另一篇文章,使用strnspn实现这一点。

Instead if you're looking for a string you can use the strstr function, like this:

相反,如果你在寻找一个字符串你可以使用strstr函数,如下所示:

char* pPosition = strstr(pText, "text to find");

#2


3  

strchr is your friend.

strchr是你的朋友。

char *strchr(const char *s, int c);

The strchr function locates the first occurrence of c (converted to a char) in the string pointed to by s.

strchr函数在s指向的字符串中查找c(转换为char)的第一次出现。

The strchr function returns a pointer to the located character, or a null pointer if the character does not occur in the string.

strchr函数返回指向所定位字符的指针,如果字符串中没有出现字符,则返回空指针。

And of course, the function has to walk through the whole string in the worst case (as the Java function probably does).

当然,在最坏的情况下,函数必须遍历整个字符串(就像Java函数可能做的那样)。

#1


11  

If you need to search for a character you can use the strchr function, like this:

如果需要搜索字符,可以使用strchr函数,如下所示:

char* pPosition = strchr(pText, '|');

pPosition will be NULL if the given character has not been found. For example:

如果没有找到给定的字符,pPosition将为空。例如:

puts(strchr("field1|field2", '|'));

Will output: "|field2". Note that strchr will perform a forward search, to search backward you can use the strrchr. Now imagine (just to provide an example) that you have a string like this: "variable:value|condition". You can extract the value field with:

将输出:“| field2”。注意,strchr将执行向前搜索,要向后搜索,可以使用strrchr。现在假设(只是提供一个示例)您有一个这样的字符串:“variable:value|条件”。可以用以下方法提取值字段:

char* pValue = strrchr(strchr(pExpression, '|'), ':') + 1;

If what you want is the index of the character inside the string take a look to this post here on SO. You may need something like IndexOfAny() too, here another post on SO that uses strnspn for this.

如果你想要的是字符串内字符的索引,请查看这里的这个帖子。您可能还需要一些类似IndexOfAny()的内容,这里有另一篇文章,使用strnspn实现这一点。

Instead if you're looking for a string you can use the strstr function, like this:

相反,如果你在寻找一个字符串你可以使用strstr函数,如下所示:

char* pPosition = strstr(pText, "text to find");

#2


3  

strchr is your friend.

strchr是你的朋友。

char *strchr(const char *s, int c);

The strchr function locates the first occurrence of c (converted to a char) in the string pointed to by s.

strchr函数在s指向的字符串中查找c(转换为char)的第一次出现。

The strchr function returns a pointer to the located character, or a null pointer if the character does not occur in the string.

strchr函数返回指向所定位字符的指针,如果字符串中没有出现字符,则返回空指针。

And of course, the function has to walk through the whole string in the worst case (as the Java function probably does).

当然,在最坏的情况下,函数必须遍历整个字符串(就像Java函数可能做的那样)。