检查字符串是否包含子字符串 [英] Check whether a string contains a substring

查看:158
本文介绍了检查字符串是否包含子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Perl检查给定的字符串是否包含某个子字符串?

How can I check whether a given string contains a certain substring, using Perl?

更具体地说,我想查看给定的字符串变量中是否存在s1.domain.com.

More specifically, I want to see whether s1.domain.com is present in the given string variable.

推荐答案

要了解字符串是否包含子字符串,可以使用 index 函数:

To find out if a string contains substring you can use the index function:

if (index($str, $substr) != -1) {
    print "$str contains $substr\n";
} 

它将返回$str中第一次出现的$substr的位置,如果未找到子字符串,则返回-1.

It will return the position of the first occurrence of $substr in $str, or -1 if the substring is not found.

这篇关于检查字符串是否包含子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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