C++ strchr 截取字符串,在协议解析中判断字符很有用

#include <string>

const char * strchr ( const char * str, int character );
char * strchr (       char * str, int character );


/* strchr example */
#include <stdio.h>
#include <string>

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
 */





你可能感兴趣的:(截取字符串)