Check whether a string contains a substring

前端 未结 3 792
挽巷
挽巷 2020-11-29 19:08

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

More specifically, I want to see whether s1.domain.com is present in th

3条回答
  •  醉酒成梦
    2020-11-29 19:41

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

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

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

提交回复
热议问题