---
title: " C++ strchr 截取字符串，在协议解析中判断字符很有用"
author: "mylove2060"
date: "2011-07-27T18:55:59Z"
tags: []
categories: ["编程语言"]
slug: "c-strchr"
draft: false
lastmod: "2011-07-27T18:56:43Z"
id: 1134280
url: "https://cooker.iteye.com/blog/1134280"
---

#include const char * strchr ( const char * str, int character ); char * strchr ( char * str, int character ); \[code="c++"] /* strchr example \*/ #include #include int main () { char str\[] = "This is a sample string"; char * pch; printf ("Looking for the 's' character in \\"%s\\"...\\n",str); pch=strchr(str,'s'); while (pch!=NULL) { printf ("found at %d\\n",pch-str+1); pch=strchr(pch+1,'s'); } return 0; } /* 输出结果 Looking for the 's' character in "This is a sample string"... found at 4 found at 7 found at 11 found at 18 \*/ \[/code]