memo->clojure.contrib.str-utils

 clojure-contrib-1.1.0のstr-utilsの一覧メモ

re-split

 文字列のパターンによる分割

(use 'clojure.contrib.str-utils)
;(doc re-split)

;正規表現
; #"hogehoge"の形式で書くと、
;   hogehoge部分が正規表現オブジェクト(regexp)に変換されるっぽい
(re-split #" " "a, b, c, d, e, f, ")  ;=> ("a," "b," "c," "d," "e," "f,")
(re-split #"," "a, b, c, d, e, f, ")  ;=> ("a" " b" " c" " d" " e" " f" " ")

;分割個数の上限設定
(re-split #"," "1,2,3,4,5" 1)   ;=> ("1,2,3,4,5")
(re-split #"," "1,2,3,4,5" 2)   ;=> ("1" "2,3,4,5")
(re-split #"," "1,2,3,4,5" 5)   ;=> ("1" "2" "3" "4" "5")
(re-split #"," "1,2,3,4,5" 6)   ;=> ("1" "2" "3" "4" "5")

re-partition

 文字列のパターンによる部分文字列への分離

;(doc re-partition) 

(re-partition #"\d+" "abc123def")    ;=> ("abc" "123" "def")
;(re-split #"\d+" "abc123def")       ;=> ("abc" "def")
(re-partition #"[a-z]+" "abc123def") ;=> ("" "abc" "123" "def")

re-gsub,re-sub

 文字列のパターンによる(一部)交換

(re-gsub #"r" "R" "rubyonrails") ;=> "RubyonRails"
(re-sub  #"r" "R" "rubyonrails") ;=> "Rubyonrails"

str-join

 文字列の結合

(str-join " , " '("Perl" "Python" "Ruby" )) ;=> "Perl , Python , Ruby"
;(apply str (interpose "," '("Perl" "Python" "Ruby" )))

chop,chomp

 終端文字の削除

(chop "abc")       ;=> "ab"
(chop "abc\n")     ;=> "abc"

(chomp "abc")      ;=> "abc"
(chomp "abc\n")    ;=> "abc"

 少ないよw;
とは言え、なんか馴染むな^^