Chinaunix首页 | 论坛 | 博客
  • 博客访问: 255570
  • 博文数量: 54
  • 博客积分: 1761
  • 博客等级: 上尉
  • 技术积分: 585
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-17 23:30
文章分类

全部博文(54)

文章存档

2013年(4)

2012年(7)

2011年(15)

2010年(28)

分类: Python/Ruby

2011-07-11 11:20:44


  • 獲取string長度
${#var}

例如
a="I am long"
echo ${#a}

切取字段
${string:position:length}或者
${string:position}
例如:
var="Welcome to the geekstuff"

echo ${var:15}
echo ${var:15:4}
刪除字符串
${string#substring} 刪除頭部開始計算的第一個符合要求的最短字符串
${string%substring} 刪除尾部開始計算的第一個符合要求的最短字符串

filename="bash.string.txt"

echo ${filename#*.}
echo ${filename%.*}

$ ./shortest.sh
After deletion of shortest match from front: string.txt
After deletion of shortest match from back: bash.string

${string##substring} 刪除頭部開始計算的符合要求的最長字符串
${string%%substring} 刪除尾部開始計算的符合要求的最長字符串
filename="bash.string.txt"

echo "After deletion of longest match from front:" ${filename##*.}
echo "After deletion of longest match from back:" ${filename%%.*}

$ ./longest.sh
After deletion of longest match from front: txt


After deletion of longest match from back: bash
獲取子串索引:
expr index $string $substring

stringZ=abcABC123ABCabc
# 123456 ...
echo `expr index "$stringZ" C12` # 6
# C position.

echo `expr index "$stringZ" 1c` # 3

# 'c' (in #3 position) matches before '1'.





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