strchr函数

函数名称:           strchr
函数原型:           char*   strchr(char*   str,char   ch);
函数功能:           找出str指向的字符串中第一次出现字符ch的位置
函数返回:           返回指向该位置的指针,如找不到,则返回空指针
参数说明:           str-待搜索的字符串,ch-查找的字符
所属文件:           <string.h>

#include   <string.h>
#include   <stdio.h>
int   main()
{
        char   string[15];
        char   *ptr,   c= 'r ';
        strcpy(string,   "This   is   a   string ");
        ptr=strchr(string,   c);
        if   (ptr)
                printf( "The   character   %c   is   at   position:   %d\n ",c,ptr-string);
        else
                printf( "The   character   was   not   found\n ");
        return   0;
}
 

---------------------------------

 

1.函数的使用

The strchr() function searches for the first occurrence of a string inside another string.
strchr()函数的作用是:返回一个字符在另一个字符串中首次出现的位置到后者末尾的子字符串(大小写敏感)。

This function returns the rest of the string (from the matching point), or FALSE, if the string to search for is not found.
如果这个函数执行成功,将返回剩余字符串(存在相匹配的字符);如果没有找到相匹配的字符,则反会False。

This function is an alias of the strstr() function.
这个函数的功能与strstr()函数相类似。

Parameter参数Description描述
stringRequired. Specifies the string to search
必要参数。指定需要搜索的字符串
searchRequired. Specifies the string to search for. If this parameter is a number, it will search for the character matching the ASCII value of the number
必要参数。指定搜索的对象。如果这个参数是一个数值,那么他将搜索与这个数值的ASCII值相匹配的字符

2.注意点

Note: This function is binary safe.
注意:strchr()函数是二进制精确[binary safe]的。

Note: This function is case-sensitive. For a case-insensitive search, use stristr().
注意:这个函数是区分字母大小写的。如果你想在不区分字母大小写的情况下进行搜索,那么可以使用stristr()函数。

3.使用举例1

 函数名: strchr 
功  能: 在一个串中查找给定字符的第一个匹配之处\ 
用  法: char *strchr(char *str, char c); 
程序例: 
#include <string.h> 
#include <stdio.h> 
int main(void) 
 { 
    char string[15]; 
    char *ptr, c = 'r'; 
    strcpy(string, "This is a string"); 
    ptr = strchr(string, c); 
    if (ptr) 
       printf("The character %c is at position: %d\n", c, ptr-string); 
    else 
       printf("The character was not found\n"); 
    return 0; 
 } 4.使用举例2:
 char buf[200];
 int cur=m_prj.GetCurSel();//得到Combox控件当前的选项"工作调查|8|9"
 m_prj.GetLBText(cur,buf);//将当前选项的值赋给buf
 char *p,*q;
 q=strchr(buf,'|');//q为|8|9
 memset(xmmc,0,sizeof(xmmc));
 strncpy(xmmc,buf,q-buf);//q的地址减去buf的地址,得到“工作调查“
 p=q;
 q++;
 q=strchr(q,'|');//q为"|9"
 memset(wjbh,0,sizeof(wjbh));
 p++;
 strncpy(wjbh,p,q-p);//得到8
 memset(xmbh,0,100);
 q++;
 strcpy(xmbh,q);//得到9
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值