用C String.indexOf函数 [英] String.indexOf function in C

查看:330
本文介绍了用C String.indexOf函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个C库函数会返回一个字符的索引在一个字符串?

Is there a C library function that will return the index of a character in a string?

到目前为止,我发现都是这样的strstr函数将返回找到的char *,而不是它在原始字符串位置。

So far, all I've found are functions like strstr that will return the found char *, not it's location in the original string.

推荐答案

我觉得

为size_t strcspn(为const char * STR1,为const char * str2的);

size_t strcspn ( const char * str1, const char * str2 );

是你想要的。下面是从这里拉一个例子:在

is what you want. Here is an example pulled from here:

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

int main ()
{
  char str[] = "fcba73";
  char keys[] = "1234567890";
  int i;
  i = strcspn (str,keys);
  printf ("The first number in str is at position %d.\n",i+1);
  return 0;
}

这篇关于用C String.indexOf函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆