Scheme的字符串操作

简介:

字符串操作是任何一门编程语言中最常用的操作之一,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"

文章转自庄周梦蝶  ,原文发布时间2009-10-12

目录
相关文章
|
网络协议 前端开发 数据安全/隐私保护
利用C语言实现URL解析的基本方法之优秀
今天主要来学习一下,如何利用URL,实现对应的解析过程。
429 0
利用C语言实现URL解析的基本方法之优秀
|
8月前
|
移动开发 Python Windows
超详细的字符串用法大全
超详细的字符串用法大全
|
9月前
|
C++
C++ string中内置的字符串操作和标准库中常用字符处理函数
C++ string中内置的字符串操作和标准库中常用字符处理函数
80 0
|
10月前
|
Python
34.从入门到精通:Python3 正则表达式检索和替换 repl 参数是一个函数 正则表达式对象 正则表达式修饰符 - 可选标志 正则表达式模式* 正则表达式实例
34.从入门到精通:Python3 正则表达式检索和替换 repl 参数是一个函数 正则表达式对象 正则表达式修饰符 - 可选标志 正则表达式模式* 正则表达式实例
|
11月前
|
C语言
C语言正则匹配库(regex.h)
C语言正则匹配库(regex.h)
273 0
在字符串方法 search() 中使用正则表达式
在字符串方法 search() 中使用正则表达式
54 0
|
文件存储 Swift
Swift5.0 - day9-字面量协议、模式匹配
Swift5.0 - day9-字面量协议、模式匹配
108 0
|
机器学习/深度学习 自然语言处理 算法