天天看點

字元串處理筆記-截取字元串

Python

Windows 7

>>> s="中國路13号弄"
>>> s.find("号")
8
>>> print s[:8]
中國路13
           

MAC OSX10.9.2

>>> s="中國路13号弄"
>>> s.find("号")
11
>>> print s[:11]
中國路13
           

Shell

Linux CentOS

從pos開始截取num個,不包含pos位置

${str:pos:num}
           

從pos開始截取l個字元,包含pos位置

expr substr "$str" 2 $num
           

忽略特殊字元

使用單引号  ' '

rm -rf 'fo o' 忽略了空格
           

cut 按字元的形式擷取前3個字元

echo $str | cut -c -3