1. 程式人生 > >字串包含關係判斷

字串包含關係判斷

1、利用grep查詢

str1="ABC"
str2="A"

result=$(echo $str1 | grep "${str2}")

if [[ "$result" != "" ]];then
    echo "包含"
else
    echo "不包含"
fi

 

2、利用字串運算子

str1="hellow"
str2="low"

if [[ $str1 =~ $str2 ]];then
    echo "包含"
else
    echo "不包含"
fi

 

3、利用萬用字元

str1="hellow"
str2
="low" if [[ $str1 == *$str2* ]];then echo "包含" else echo "不包含" fi