STRCHR vs STRRCHR difference? [closed]

我只是一个虾纸丫 提交于 2019-12-01 22:54:39

问题


I would like to know the difference between the two different uses. I believe the difference in some what very subtle.

This is an explanation taken from the IBM reference manual. However maybe my english is bad, I just can't visualize the difference.

Maybe showing me an example of both cases would help me understand this better.

Here is the explanation from IBM :

The strchr subroutine returns a pointer to the first occurrence of the character specified by the Character (converted to an unsigned character) parameter in the string pointed to by the String parameter. A null pointer is returned if the character does not occur in the string. The null byte that terminates a string is considered to be part of the string.

The strrchr subroutine returns a pointer to the last occurrence of the character specified by the Character (converted to a character) parameter in the string pointed to by the String parameter. A null pointer is returned if the character does not occur in the string. The null byte that terminates a string is considered to be part of the string.


回答1:


strchr is an alias of strstr:

Returns part of haystack string starting from and including the first occurrence of needle to the end of haystack.

strrchr:

This function returns the portion of haystack which starts at the last occurrence of needle and goes until the end of haystack.

strchr starts at the first occurrence, strrchr starts at the last occurrence.




回答2:


strchr finds the first occurrence and strrchr (r - reverse) finds the last occurrence of a character or string

$s = 'This is a test';
$first = strchr($s, 's');
$last = strrchr($s, 's');
  • $first contains s is a test
  • $last contains st


来源:https://stackoverflow.com/questions/15725560/strchr-vs-strrchr-difference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!