方法一:利用字符串运算符
str='this is a tree! and that is a car.'
//如果包含this
[[ $str =~ "this" ]] && echo "\$str contains this"
//如果不包含that
[[ $str =~ "that" ]] || echo "\$str does NOT contain that"
数组参数引用的话,可以使用${result[*]} *的话数组中的数据都会遍历;
方法二:利用grep查找
strA="long string"
strB="string"
result=$(echo $strA | grep "${strB}")
if [[ "$result" != "" ]]
then
echo "包含"
else
echo "不包含"
fi
先打印长字符串,然后在长字符串中 grep 查找要搜索的字符串,用变量result记录结果
如果结果不为空,说明strA包含strB。如果结果为空,说明不包含。
方法三:利用通配符
A="helloworld"
B="low"
if [[ $A == *$B* ]]
then
echo "包含"
else
echo "不包含"
fi
这个也很easy,用通配符*号代理strA中非strB的部分,如果结果相等说明包含,反之不包含。
方法四:利用case in 语句
thisString="1 2 3 4 5" # 源字符串
searchString="1 2" # 搜索字符串
case $thisString in
*"$searchString"*) echo Enemy Spot ;;
*) echo nope ;;
esa
方法五:利用替换
STRING_A=$1
STRING_B=$2
if [[ ${STRING_A/${STRING_B}//} == $STRING_A ]]
then
## is not substring.
echo N
return 0
else
## is substring.
echo Y
return 1
fi