莊周夢(mèng)蝶

          生活、程序、未來(lái)
             :: 首頁(yè) ::  ::  :: 聚合  :: 管理

          Scheme的字符串操作

          Posted on 2009-10-12 17:59 dennis 閱讀(1913) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): 動(dòng)態(tài)語(yǔ)言
              字符串操作是任何一門(mén)編程語(yǔ)言中最常用的操作之一,scheme也提供了一系列procudure來(lái)操作字符串。

          1、字符串的比較,有6個(gè),分別是string=?  string>? string<? string>=? string<=?

          這與其他語(yǔ)言中對(duì)string的比較并無(wú)不同,比較字符和長(zhǎng)度。

          例子:
          (string=? "mom" "mom") <graphic> #t
          (string<? "mom" "mommy") <graphic> #t
          (string>? "Dad" "Dad") <graphic> #f
          (string=? "Mom and Dad" "mom and dad") <graphic> #f
          (string<? "a" "b" "c") <graphic> #t

          注意這些比較操作是大小寫(xiě)敏感。相應(yīng)的,大小寫(xiě)不敏感的版本:

          procedure: (string-ci=? string1 string2 string3 ...)
          procedure: (string-ci<? string1 string2 string3 ...)
          procedure: (string-ci>? string1 string2 string3 ...)
          procedure: (string-ci<=? string1 string2 string3 ...)
          procedure: (string-ci>=? string1 string2 string3 ...)

          2、從字符構(gòu)造字符串,使用string過(guò)程
          (string #\a)  => "a"
          (string #\a #\b #\c)  => "abc"

          注意,換行字符是#\newline,回車(chē)字符是#\return

          3、重復(fù)N個(gè)字符構(gòu)造字符串
          (make-string)  => ""
          (make-string 4 #\a)  =>"aaaa")

          4、字符串長(zhǎng)度 string-length
          (string-length "") =>0
          (string-length "dennis") => 6

          5、取第N個(gè)字符,相當(dāng)于java中的charAt:

          (string-ref "hi there" 0) <graphic> #\h
          (string-ref "hi there" 5) <graphic> #\e

          6、修改字符串的第N個(gè)字符:
          (string-set! "hello" 0 #\H) => "Hello"

          7、拷貝字符串:
          (let ((str "abc"))
            (eq? str (string-copy str)))  => #f
          (let ((str "abc"))
            (equal? str (string-copy str)))  => #t

          8、拼接字符串,string-append
          (string-append) => ""
          (string-append "abc" "defg") => "abcdefg"

          9、截取子串
          (substring "hi there" 0 1) <graphic> "h"
          (substring "hi there" 3 6) <graphic> "the"
          (substring "hi there" 5 5) <graphic> ""

          10、填充字符串
          (let ((str (string-copy "sleepy")))
            (string-fill! str #\Z)
            str) <graphic> "ZZZZZZ"

          11、與list的相互轉(zhuǎn)換

          (string->list "") <graphic> ()
          (string->list "abc") <graphic> (#\a #\b #\c)

          (list->string '()) <graphic> ""
          (list->string '(#\a #\b #\c)) <graphic> "abc"
          (list->string
            (map char-upcase
                 (string->list "abc"))) <graphic> "ABC"

          主站蜘蛛池模板: 富平县| 友谊县| 泾源县| 德清县| 沂南县| 阳谷县| 皮山县| 花垣县| 霍林郭勒市| 天等县| 济宁市| 应用必备| 张家界市| 武山县| 加查县| 巴彦淖尔市| 甘德县| 合水县| 九江市| 固镇县| 蚌埠市| 信丰县| 汾西县| 图木舒克市| 塘沽区| 靖边县| 平武县| 黄浦区| 通山县| 五寨县| 阳山县| 乐安县| 博乐市| 巨鹿县| 滕州市| 中江县| 临汾市| 拜泉县| 盐津县| 日喀则市| 金坛市|