标签:liuyifei position swimming 字符串
字符串操作
${#string}输出字句的字符个数
${string:position:length}输出$position开始的length长度的字符
${string:position}
[root@ilove scripts]# liuyifei="i like sanpan i like luo swimming" [root@ilove scripts]# echo ${#liuyifei} 33 [root@ilove scripts]# echo ${liuyifei:2:4} like [root@ilove scripts]# echo ${liuyifei:2} like sanpan i like luo swimming ====以上实测 ${string#substring}从string中从前往后检索,只要匹配到字符就开始删除最短匹配 ${string##substring}从头匹配,开始最长删除 ${string%substring}从结尾开始 ${string%%substring}从结尾开始,类似以上## ======以上必须必须从一开始就匹配上 [root@ilove scripts]# echo ${liuyifei#like} i like sanpan i like luo swimming [root@ilove scripts]# echo ${liuyifei#*like} sanpan i like luo swimming [root@ilove scripts]# echo ${liuyifei##*like} luo swimming [root@ilove scripts]# echo ${liuyifei%*like} i like sanpan i like luo swimming [root@ilove scripts]# echo ${liuyifei%like} i like sanpan i like luo swimming [root@ilove scripts]# echo ${liuyifei%like*} i like sanpan i [root@ilove scripts]# echo ${liuyifei%%like*} i
========================
${string/substring/replace}替换第一个遇到的匹配的字符
${string/#substring/replace}前缀匹配,必须全部匹配上,替换一长条
${string/%substring/replace}从后往前,替换最后一个遇到的匹配字符,
[root@ilove scripts]# echo ${liuyifei/like/love}
i love sanpan i like luo swimming
[root@ilove scripts]# echo ${liuyifei/#*like/love}
love luo swimming
[root@ilove scripts]# echo ${liuyifei/%like*/love}
i love
本文出自 “Linux学习小结0613” 博客,谢绝转载!
标签:liuyifei position swimming 字符串
原文地址:http://singler.blog.51cto.com/3984787/1812413