返回c中字符串中子字符串的开头 [英] return the start of a substring in a string in c

查看:114
本文介绍了返回c中字符串中子字符串的开头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在java中,String中有一个''indexOf''函数可以做到这一点:


indexOf(String str)

返回指定子字符串第一次出现

的字符串中的索引。


c中是否有类似的内容?


我能找到的最接近的是strchr,但它检查一个字符,

不是子字符串?


谢谢。

Hi,
In java, there is an ''indexOf'' function in String which does this:

indexOf(String str)
Returns the index within this string of the first occurrence
of the specified substring.

is there anything like that in c?

The closest thing I can find is strchr, but it check for a character,
not a substring?

Thank you.

推荐答案

yi * **** @ gmail.com 说:



在java中,有一个''indexOf' '函数在String中执行此操作:


indexOf(String str)

返回第一次出现的字符串中的索引
$ b $指定子串的b。


c中有类似的东西吗?


我能找到的最接近的是strchr,但它检查为了一个charac ter,

不是子串?
Hi,
In java, there is an ''indexOf'' function in String which does this:

indexOf(String str)
Returns the index within this string of the first occurrence
of the specified substring.

is there anything like that in c?

The closest thing I can find is strchr, but it check for a character,
not a substring?



strstr返回指向子字符串的指针 - 如果这是非NULL,则

从中减去主字符串的地址以获取指数。


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

strstr returns a pointer to the substring - if this is non-NULL,
subtract the address of the main string from it to get the index.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999




" Richard Heathfield" < rj*@see.sig.invalidwrote in message

news:tZ ********************* @ bt.com .. 。

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:tZ*********************@bt.com...
yi*****@gmail.com 说:

>
在java中,String中有一个''indexOf''函数可以做到这一点:

indexOf( String str)
返回指定子字符串第一次出现的字符串中的索引。

在c中有类似的东西吗?

我能找到最接近的东西是strchr,但它会检查一个角色,
不是子串?
>Hi,
In java, there is an ''indexOf'' function in String which does this:

indexOf(String str)
Returns the index within this string of the first occurrence
of the specified substring.

is there anything like that in c?

The closest thing I can find is strchr, but it check for a character,
not a substring?



strstr返回指向子字符串的指针 - 如果这是非NULL,

从中减去主字符串的地址以获取索引。


strstr returns a pointer to the substring - if this is non-NULL,
subtract the address of the main string from it to get the index.



ie;


char * str =" astring";

char * substring =" in" ;;

char * ptr;

ptrdiff_t index; / *应该是一个int。可怕的可怕的恐怖* /

ptr = strstr(str,substring);

if(ptr)

{

index = ptr - str;

printf(你的子字符串是在位置%d找到的(从0开始)\ n,(int)

index);

}


作为对regs的练习,为什么我们需要在
$ b中进行可怕的,可怕的演员表演$ b打电话给printf?

-

免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/~bgy1mm

< br <>

Malcolm McLean写道:
Malcolm McLean wrote:

char * str =" astring" ;;

char * substring =" in" ;;

char * ptr;

ptrdiff_t index; / *应该是一个int。可怕的可怕的恐怖* /

ptr = strstr(str,substring);

if(ptr)

{

index = ptr - str;

printf(你的子字符串是在位置%d找到的(从0开始)\ n,(int)

index);

}


作为对regs的练习,为什么我们需要在
$ b中进行可怕的,可怕的演员表演$ b打电话给printf?
char *str = "astring";
char *substring = "in";
char *ptr;
ptrdiff_t index; /* should be an int. Horrid horrid horrid */

ptr = strstr(str, substring);
if(ptr)
{
index = ptr - str;
printf("your substring was found at postion %d (0-based)\n", (int)
index);
}

As an exercise to the regs, why do we need that horrid, horrid cast in the
call to printf?



你不是。你已经假定索引适合int,所以你可以

只需更改索引的声明即可。

You don''t. You''re already assuming that index will fit in an int, so you can
just change the declaration of index to match.


这篇关于返回c中字符串中子字符串的开头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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