在给定的字符串中搜索模式 [英] Search a pattern in given string

查看:99
本文介绍了在给定的字符串中搜索模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我编写了一个函数,该函数在给定的字符串中搜索给定的模式,并且
如果模式在函数中,则分别返回是",而否"则返回.另外,它还返回发生图案的位置.例如,字符串"hello world"包含从位置6开始的模式"wor".

如果您能看一下我的代码并为我提供反馈,是否可以对其进行进一步的改进或优化,我将感到非常高兴.谢谢你. (附言:代码当然是有效的,如果可能的话,我只是对优化感兴趣).
...

Hey folks,

I have written a function which searches for a given pattern in a given string and
returns yes if the pattern is in the function and no respectively. Plus it also returns the position from where the pattern occurred. e.g., string "hello world" contains pattern "wor" starting from position: 6.

I would be very glad if you could look at my code and provide me with feedback whether it could be more improved or optimized???? Thank you. (ps. the code is working of course, just I am interested in optimization if it is possible).
...

推荐答案

您还可以检查库中的标准功能,例如
You could also check the libraries for standard functions such as strstr()[^].


您可以使用上面提到的strstr(),或者如果您使用的是std :: string,请使用 30分钟正则表达式教程 [ ^ ]
You can go with the mentioned strstr() or if you are using std::string then use std::string::find()[^].

For searching very complex patterns use regular expressions: The 30 Minute Regex Tutorial[^]


这篇关于在给定的字符串中搜索模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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