Chinaunix首页 | 论坛 | 博客
  • 博客访问: 80056
  • 博文数量: 32
  • 博客积分: 1526
  • 博客等级: 上尉
  • 技术积分: 270
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-28 13:56
文章分类

全部博文(32)

文章存档

2012年(1)

2011年(15)

2010年(1)

2009年(15)

我的朋友
最近访客

分类:

2009-11-14 10:16:10

#!/bin/bash
#=====================================================
#operate_string.sh
#This script teach us some operations about strings.
#=====================================================
#Version 1.0\2009-11-10\by zhengyu
#Any question can be emailed to
#=====================================================
string=abcdeABCDEABCDE12345
#      123456789......
substring1=abc[A-Z]*
substring2=3E
echo "==============================================="
echo "Operation 1:match
 expr match \"\$string\" '\$substring'"
echo "Result:"
echo -e " `expr match $string $substring1`"
echo "==============================================="
echo "Operation 2:index
 expr match \"\$string\" '\$substring'"
echo "Result:"
echo -e " `expr index $string $substring2`"
echo "==============================================="
echo "Operation 3:
 \${string:position}"
echo "Result:"
echo -e " ${string:2}"
echo -e " ${string:-2}"
echo -e " ${string:-default}"
echo -e " ${string:(-2)}"
echo -e " ${string: -2}"
echo "==============================================="
echo "Operation 4:
 \${string:position:length}"
echo "Result:"
echo -e " ${string:2:4}"
echo -e " ${string:(-5):4}"
echo -e " ${string:-5:4)}"
echo -e " ${string:-5}"
echo -e " ${string:(-5):7}"
echo "==============================================="
echo "Operation 5:substr
 expr substr \$string \$position \$length"
echo "Result:"
echo -e " `expr substr $string 2 5`"
echo "==============================================="
echo "Operation 6:
 expr match "\$string" '\(\$substring\)'
 expr "\$string" : '\(\$substring\)'"
echo "Result:"
echo -e " `expr match $string '\(.[a-z]*[A-Z]*[0-9]\)'`"
echo -e " `expr $string : '\(.[a-z]*[A-Z]*[0-9]\)'`"
echo -e " `expr $string : '.*\(.....\)'`"
echo "==============================================="
echo "Operation 7:
 expr match "\$string" '.*\(\$substring\)'
 expr "\$string" : '.*\(\$substring\)'"
echo "Result:"
echo -e " `expr match "$string" '.*\([a-z][A-Z][0-9]*\)'`"
echo -e " `expr $string : '.*\([a-z][A-Z][0-9]*\)'`"
echo -e " `expr $string : '.*\(...\)'`"
echo "==============================================="
echo "Operation 8:
 \${string#substring}
 \${string##substring}"
echo "Result:"
echo -e " ${string#a*E}"
echo -e " ${string##a*E}"
echo "==============================================="
echo "Operation 9:
 \${string%substring}
 \${string%%substring}"
echo "Result:"
echo -e " ${string%E*5}"
echo -e " ${string%%E*5}"
exit 0
 
 
运行结果:
===============================================
Operation 1:match
 expr match "$string" '$substring'
Result:
 3
===============================================
Operation 2:index
 expr match "$string" '$substring'
Result:
 10
===============================================
Operation 3:
 ${string:position}
Result:
 cdeABCDEABCDE12345
 abcdeABCDEABCDE12345
 abcdeABCDEABCDE12345
 45
 45
===============================================
Operation 4:
 ${string:position:length}
Result:
 cdeA
 1234
 abcdeABCDEABCDE12345
 abcdeABCDEABCDE12345
 12345
===============================================
Operation 5:substr
 expr substr $string $position $length
Result:
 bcdeA
===============================================
Operation 6:
 expr match $string '\($substring\)'
 expr $string : '\($substring\)'
Result:
 abcdeABCDEABCDE1
 abcdeABCDEABCDE1
 12345
===============================================
Operation 7:
 expr match $string '.*\($substring\)'
 expr $string : '.*\($substring\)'
Result:
 eA
 eA
 345
===============================================
Operation 8:
 ${string#substring}
 ${string##substring}
Result:
 ABCDE12345
 12345
===============================================
Operation 9:
 ${string%substring}
 ${string%%substring}
Result:
 abcdeABCDEABCD
 abcdeABCD
阅读(771) | 评论(0) | 转发(0) |
0

上一篇:计算字符串长度

下一篇:ftp的一个小问题

给主人留下些什么吧!~~