Scheme的字符串操作
字符串操作是任何一门编程语言中最常用的操作之一,scheme也提供了一系列procudure来操作字符串。
1、字符串的比较,有6个,分别是string=? string>? string<? string>=? string<=?
这与其他语言中对string的比较并无不同,比较字符和长度。
例子:
(string=? "mom" "mom") #t
(string<? "mom" "mommy") #t
(string>? "Dad" "Dad") #f
(string=? "Mom and Dad" "mom and dad") #f
(string<? "a" "b" "c") #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) #\h
(string-ref "hi there" 5) #\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) "h"
(substring "hi there" 3 6) "the"
(substring "hi there" 5 5) ""
10、填充字符串
(let ((str (string-copy "sleepy")))
(string-fill! str #\Z)
str) "ZZZZZZ"
11、与list的相互转换
(string->list "") ()
(string->list "abc") (#\a #\b #\c)
(list->string '()) ""
(list->string '(#\a #\b #\c)) "abc"
(list->string
(map char-upcase
(string->list "abc"))) "ABC"
文章转自庄周梦蝶 ,原文发布时间2009-10-12
相关文章
- 使用 Elasticsearch OSS 构建和运行 Open Distro For Elasticsearch SQL 插件
- 更改 Open Distro for Elasticsearch 中的 Admin 密码
- 使用 Rancher 管理 EKS 集群
- 启动并运行 Open Distro for Elasticsearch
- 使用 Amazon EC2 M5 和 R5 实例提升 Amazon ElastiCache 性能
- 新推 – 适用于 Amazon Direct Connect 的千兆位连接选项
- 将 AWS IAM Authenticator 部署到 kops
- Western Digital HDD 云级模拟 – 250 万个 HPC 任务,4 万个 EC2 Spot 实例
- 使用AWS Sagemaker部署的终端节点进行推荐预测的常用场景
- 使用 Terraform 在 AWS 中国区域实现自动化部署指南系列(二) TERRAFORM 进阶
- AWS 精英:让 AWS 安全服务为您服务
- 在 Amazon EKS 上使用 Open Policy Agent
- 保持开源的开放性 – Open Distro for Elasticsearch
- Java GUI开发
- python pdf转markdown
- Lucidchart 如何让您更轻松地创建基础架构图 | AWS 初创公司博客
- 借助 Multi-Factor Authentication 保护实例安全 | AWS 初创公司博客
- 使用 AWS 区域服务实现大规模灾难恢复 | AWS 初创公司博客
- JAVA100例
- 如何使用 Amazon CloudFront 加速您的 WordPress 站点 | AWS 初创公司博客