Chinaunix首页 | 论坛 | 博客
  • 博客访问: 210298
  • 博文数量: 145
  • 博客积分: 3000
  • 博客等级: 中校
  • 技术积分: 1720
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-14 18:42
文章分类

全部博文(145)

文章存档

2011年(1)

2009年(144)

我的朋友

分类: LINUX

2009-10-19 10:13:22

by tangke 2009-10-19

1.字符串截取
export test="get the length of string"

1.echo ${test:0:3}
截取test中第0个到第2个的字符串
g算第0个字符

2.echo ${test%%*}
无效
3.echo ${test%% *}
get
截取左边最后一个空格的左边的字符串
4.echo ${test%% * }
无效
5.echo ${test%%of*}
get the length
截取左边最后一个of的左边的的字符串
6.echo ${test%%*of}
无效
7.echo ${test%of*}
get the length
截取左边第一个of的左边的字符串
8.echo ${test% *}
get the length of
截取左边第一个空格的左边的字符串

9.echo ${test##* }
string
截取右边最后一个空格的右边的字符串
10.echo ${test##*of}
string
截取右边最后一个of的右边的字符串
11.echo ${test#* }
the length of string
截取右边第一个空格的右边的字符串
12.echo ${test#*of}
string
截取右边第一个of的右边的字符串

2.字符串替换
1.echo ${test/ /_}
get_the length of string
2.echo ${test// /_}
get_the_length_of_string

3.字符串添加
1.echo ${test/ / X}
get Xthe length of string
2.echo ${test// / X}
get Xthe Xlength Xof Xstring

4.字符串删除
1.echo ${test/ /}
getthe length of string
2.echo ${test// /}
getthelengthofstring


下面是一个测试脚本:
#!/bin/bash

test="get the length of string"
echo "截取"
echo "echo \${test:0:3}"
echo ${test:0:3}

echo "echo \${test%% *}"
echo ${test%% *}
echo "echo \${test% *}"
echo ${test% *}
echo "echo \${test##* }"
echo ${test##* }
echo "echo \${test#* }"
echo ${test#* }

echo "替换"
echo "echo \${test/ /_}"
echo ${test/ /_}
echo "echo \${test// /_}"
echo ${test// /_}

echo "删除"
echo "echo \${test/ /}"
echo ${test/ /}
echo "echo \${test// /}"
echo ${test// /}

echo "插入"
echo "echo \${test/ / X}"
echo ${test/ / X}
echo "echo \${test// / X}"
echo ${test// / X}


阅读(304) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~