-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module (chicken string)
170 lines (106 loc) · 4.72 KB
/
Module (chicken string)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
[[tags: manual]]
[[toc:]]
== Module (chicken string)
This module contains procedures which can perform various useful
string operations.
=== conc
<procedure>(conc X ...)</procedure>
Returns a string with the string-represenation of all arguments
concatenated together. {{conc}} could be implemented as
<enscript highlight=scheme>
(define (conc . args)
(apply string-append (map ->string args)) )
</enscript>
=== ->string
<procedure>(->string X)</procedure>
Returns a string-representation of {{X}}.
=== string-chop
<procedure>(string-chop STRING LENGTH)</procedure>
Returns a list of substrings taken by ''chopping'' {{STRING}} every {{LENGTH}}
characters:
<enscript highlight=scheme>
(string-chop "one two three" 4) ==> ("one " "two " "thre" "e")
</enscript>
=== string-chomp
<procedure>(string-chomp STRING [SUFFIX])</procedure>
If {{STRING}} ends with {{SUFFIX}}, then this procedure returns a copy
of its first argument with the suffix removed, otherwise returns
{{STRING}} unchanged. {{SUFFIX}} defaults to {{"\n"}}.
=== string-compare3
<procedure>(string-compare3 STRING1 STRING2)</procedure><br>
<procedure>(string-compare3-ci STRING1 STRING2)</procedure>
Perform a three-way comparison between the {{STRING1}} and {{STRING2}},
returning either {{-1}} if {{STRING1}} is lexicographically less
than {{STRING2}}, {{0}} if it is equal, or {{1}} if it s greater.
{{string-compare3-ci}} performs a case-insensitive comparison.
=== string-intersperse
<procedure>(string-intersperse LIST [STRING])</procedure>
Returns a string that contains all strings in {{LIST}} concatenated
together. {{STRING}} is placed between each concatenated string and
defaults to {{" "}}.
<enscript highlight=scheme>
(string-intersperse '("one" "two") "three")
</enscript>
is equivalent to
<enscript highlight=scheme>
(apply string-append (intersperse '("one" "two") "three"))
</enscript>
=== string-split
<procedure>(string-split STRING [DELIMITER-STRING [KEEPEMPTY]])</procedure>
Split string into substrings delimited by any of the characters given
in the delimiter string. If no delimiters are specified, a string
comprising the tab, newline and space characters is assumed. If the
parameter {{KEEPEMPTY}} is given and not {{#f}}, then empty substrings
are retained:
<enscript highlight=scheme>
(string-split "one two three") ==> ("one" "two" "three")
(string-split "foo:bar::baz:" ":" #t) ==> ("foo" "bar" "" "baz" "")
(string-split "foo:bar:baz,quux,zot" ":," ) ==> ("foo" "bar" "baz" "quux" "zot")
</enscript>
=== string-translate
<procedure>(string-translate STRING FROM [TO])</procedure>
Returns a fresh copy of {{STRING}} with characters matching
{{FROM}} translated to {{TO}}. If {{TO}} is omitted, then
matching characters are removed. {{FROM}} and {{TO}} may be
a character, a string or a list. If both {{FROM}} and {{TO}}
are strings, then the character at the same position in {{TO}}
as the matching character in {{FROM}} is substituted.
=== string-translate*
<procedure>(string-translate* STRING SMAP)</procedure>
Substitutes elements of {{STRING}} according to {{SMAP}}.
{{SMAP}} should be an association-list where each element of the list
is a pair of the form {{(MATCH . REPLACEMENT)}}. Every occurrence of
the string {{MATCH}} in {{STRING}} will be replaced by the string
{{REPLACEMENT}}:
<enscript highlight=scheme>
(string-translate*
"<h1>this is a \"string\"</h1>"
'(("<" . "<") (">" . ">") ("\"" . """)) )
=> "<h1>this is a "string"</h1>"
</enscript>
=== substring=?
<procedure>(substring=? STRING1 STRING2 [START1 [START2 [LENGTH]]])</procedure><br>
<procedure>(substring-ci=? STRING1 STRING2 [START1 [START2 [LENGTH]]])</procedure>
Returns {{#t}} if the strings {{STRING1}} and {{STRING2}} are equal,
or {{#f}} otherwise. The comparison starts at the positions
{{START1}} and {{START2}} (which default to 0), comparing {{LENGTH}}
characters (which defaults to the minimum of the remaining length of
both strings).
=== substring-index
<procedure>(substring-index WHICH WHERE [START])</procedure><br>
<procedure>(substring-index-ci WHICH WHERE [START])</procedure>
Searches for first index in string {{WHERE}} where string
{{WHICH}} occurs. If the optional argument {{START}} is given,
then the search starts at that index. {{substring-index-ci}}
is a case-insensitive version of {{substring-index}}.
=== reverse-list->string
<procedure>(reverse-list->string LIST)</procedure>
Returns a string with the characters in {{LIST}} in reverse
order. This is equivalent to {{(list->string (reverse LIST))}}, but
much more efficient.
=== reverse-string-append
<procedure>(reverse-string-append LIST)</procedure>
{{(apply string-append (reverse LIST))}}
---
Previous: [[Module (chicken sort)]]
Next: [[Module (chicken syntax)]]