莊周夢蝶

          生活、程序、未來
             :: 首頁 ::  ::  :: 聚合  :: 管理

          Scheme的字符串操作

          Posted on 2009-10-12 17:59 dennis 閱讀(1913) 評論(0)  編輯  收藏 所屬分類: 動態語言
              字符串操作是任何一門編程語言中最常用的操作之一,scheme也提供了一系列procudure來操作字符串。

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

          這與其他語言中對string的比較并無不同,比較字符和長度。

          例子:
          (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

          注意這些比較操作是大小寫敏感。相應的,大小寫不敏感的版本:

          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、從字符構造字符串,使用string過程
          (string #\a)  => "a"
          (string #\a #\b #\c)  => "abc"

          注意,換行字符是#\newline,回車字符是#\return

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

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

          5、取第N個字符,相當于java中的charAt:

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

          6、修改字符串的第N個字符:
          (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的相互轉換

          (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"

          主站蜘蛛池模板: 攀枝花市| 探索| 农安县| 南召县| 和田县| 宣恩县| 准格尔旗| 临朐县| 辛集市| 长海县| 河南省| 永康市| 东山县| 永安市| 凤阳县| 抚松县| 襄垣县| 井陉县| 西畴县| 资溪县| 化德县| 昆明市| 肇东市| 屏东市| 岳池县| 寿宁县| 金平| 吉安县| 仪陇县| 玉龙| 昌都县| 政和县| 巫山县| 安新县| 都昌县| 濮阳市| 惠东县| 沅江市| 石棉县| 江永县| 安乡县|