目录
一、字符串匹配
strA="helloworld"
strB="world"
#方法一 利用字符串运算符=~
if [[ $strA =~ $strB ]]
then
echo "包含world"
else
echo "不包含world"
fi
#方法二 利用通配符
if [[ $strA == *$strB* ]]
then
echo "包含world"
else
echo "不包含world"
fi
二、字符串替换
strA="helloworld"
#删除最后一个world
strB=${strA%world}
echo strB: $strB
#strB: hello
#删除最后一个w及右边字符串
strB=${strA%w*}
echo strB: $strB
#strB: hello
#删除第一个0及左边字符串
strB=${strA#*o}
echo strB: $strB
#strB: world
#删除最后一个0及左边字符串
strB=${strA##*o}
echo strB: $strB
#strB: rld
# # 是 去掉左边(键盘上#在 $ 的左边)
# %是去掉右边(键盘上% 在$ 的右边)
#将第一个o替换为F
strB=${strA/o/F}
echo strB: $strB
#strB: hellFworld
#将全部o替换为F
strB=${strA//o/F}
echo strB: $strB
#strB: hellFwFrld
三、计算字符串长度
strA="helloworld"
strB=${#strA}
echo strB: $strB
#strB: 10
四、判断字符串是否为空
if [ -z "$strA" ]
then
echo "strA is empty"
else
echo "strA is NOT empty"
fi