ruby字符串String的一個trick
用了這麼久ruby,知道String對象可以通過[]操作得到字符或者子字符串,比如:
>"abc"[0]
97
>"abc"[0,2]
"ab"
97
>"abc"[0,2]
"ab"
97就是字符a的ASCII碼了,卻不知道[]操作同樣可以接受正則表達式,返回匹配正則的那部分字符串,比如:
>"has 5 and 3" [/\d+/]
5
>"hello there"[/(..)e/]
the
5
>"hello there"[/(..)e/]
the
ruby的API設計充分體現了馬教主所說的人本接口。
文章轉自莊周夢蝶 ,原文發布時間2007-7-6
最後更新:2017-05-17 15:31:21