```powershell
:<<eof
字符串处理
1. 获取字符串长度
- ${
- expr length "$string"
> string内容有空格的话,则必须加上""
2. 获取子串在字符串中的索引位置(了解)
expr index "$string" $substring
会将substring分割成一个个字符,在string中进行逐个匹配,所有匹配的索引取最小值
3. 计算子串长度(了解)
expr match "$string" substr
substr必须是从string头开始,否则匹配不成功
4. 抽取子串
- ${string: position} 从string中的position开始(索引下标从0开始)
- ${string: position:length} 从position开始,匹配长度为length
- ${string: -position:length} 注意空格,从右边开始匹配
- expr substr $string $position $length 从position开始,匹配长度为length(索引下标从1开始)
eof
set -e
a="beautiful"
length=${
for((i=0;i<length;i++))
do
echo ${a:i:1}
done
${a:5:3}
${a: -5:3}