linux 字符串截取

lishell中截取字符串的方法很多

[
img_51e409b11aa51c150090697429a953ed.gif
li

](javascript:void(0); "复制代码")

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">{var#*/}{var##/}
{var%/*}{var%%/
}
{var:start:len}{var:start}
{var:0-start:len}{var:0-start}</pre>

[
img_51e409b11aa51c150090697429a953ed.gif
复制代码

](javascript:void(0); "复制代码")


下面用几个例子展示一下:

1) 获得字符串的长度

语法:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">${#var}</pre>

示例代码:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [{str}]" length={#str} echo "length : [${length}]"</pre>

执行结果:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
length : [61]</pre>


2) 使用 # 和 ## 获取尾部子字符串

2.1) # 最小限度从前面截取word

语法:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">${parameter#word} </pre>

示例代码:

[
img_51e409b11aa51c150090697429a953ed.gif
复制代码

](javascript:void(0); "复制代码")

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [{str}]" #分割符为'/' substr={str#*/} echo "substr : [${substr}]"</pre>

[
img_51e409b11aa51c150090697429a953ed.gif
复制代码

](javascript:void(0); "复制代码")

执行结果:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [/www.fengbohello.xin3e.com/blog/shell-truncating-string]</pre>

2.2) ## 最大限度从前面截取word

语法:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">${parameter##word}</pre>

示例代码:

[
img_51e409b11aa51c150090697429a953ed.gif
复制代码

](javascript:void(0); "复制代码")

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [{str}]" #分割符为'/' substr={str##*/} echo "substr : [${substr}]"</pre>

[
img_51e409b11aa51c150090697429a953ed.gif
复制代码

](javascript:void(0); "复制代码")

执行结果:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [shell-truncating-string]</pre>


3) 使用 % 和 %% 获取头部子字符串

3.1) % 最小限度从后面截取word

语法:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">${parameter%word} </pre>

示例代码:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [{str}]" substr={str%/*}
echo "substr : [${substr}]"</pre>

执行结果:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [http://www.fengbohello.xin3e.com/blog]</pre>

3.2) %% 最大限度从后面截取word

语法:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">${parameter%%word}</pre>

示例代码:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [{str}]" substr={str%%/*}
echo "substr : [${substr}]"</pre>

执行结果:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [http:]</pre>


4)使用 ${var:} 模式获取子字符串

4.1) 指定从左边第几个字符开始以及子串中字符的个数

语法:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">${var:start:len}</pre>

示例代码:

[
img_51e409b11aa51c150090697429a953ed.gif
复制代码

](javascript:void(0); "复制代码")

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [{str}]" #其中的 0 表示左边第一个字符开始,7 表示子字符的总个数。 substr={str:0:7} echo "substr : [${substr}]"</pre>

[
img_51e409b11aa51c150090697429a953ed.gif
复制代码

](javascript:void(0); "复制代码")

执行结果:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [http://]</pre>

4.2) 从左边第几个字符开始一直到结束

语法:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">${var:7}</pre>

示例代码:

[
img_51e409b11aa51c150090697429a953ed.gif
复制代码

](javascript:void(0); "复制代码")

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [{str}]" #其中的 7 表示左边第8个字符开始 substr={str:7} echo "substr : [${substr}]"</pre>

[
img_51e409b11aa51c150090697429a953ed.gif
复制代码

](javascript:void(0); "复制代码")

执行结果:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [www.fengbohello.xin3e.com/blog/shell-truncating-string]</pre>

4.3) 从右边第几个字符开始以及字符的个数

语法:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">${var:0-start:len}</pre>

示例代码:

[
img_51e409b11aa51c150090697429a953ed.gif
复制代码

](javascript:void(0); "复制代码")

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [{str}]" #其中的 0-23 表示右边算起第23个字符开始,5 表示字符的个数 substr={str:0-23:5} echo "substr : [${substr}]"</pre>

[
img_51e409b11aa51c150090697429a953ed.gif
复制代码

](javascript:void(0); "复制代码")

执行结果:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [shell]</pre>

4.4) 从右边第几个字符开始一直到结束

语法:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">${var:0-start}</pre>

示例代码:

[
img_51e409b11aa51c150090697429a953ed.gif
复制代码

](javascript:void(0); "复制代码")

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">str="http://www.fengbohello.xin3e.com/blog/shell-truncating-string"
echo "string : [{str}]" #其中的 0-6 表示右边算起第6个字符开始 substr={str:0-6} echo "substr : [${substr}]"</pre>

[
img_51e409b11aa51c150090697429a953ed.gif
复制代码

](javascript:void(0); "复制代码")

执行结果:

<pre style="margin: 0px; padding: 0px; white-space: pre-wrap; overflow-wrap: break-word; font-family: "Courier New" !important; font-size: 12px !important;">string : [http://www.fengbohello.xin3e.com/blog/shell-truncating-string]
substr : [string]</pre>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值