0
# String Processing
1
2
Comprehensive string manipulation utilities through the clojure.string namespace. All functions treat strings as immutable and return new strings.
3
4
## Capabilities
5
6
### Basic String Operations
7
8
Core string manipulation functions for common text processing tasks.
9
10
```clojure { .api }
11
(clojure.string/join coll)
12
(clojure.string/join separator coll)
13
;; Returns string of all elements in coll, optionally separated by separator
14
;; Returns: String
15
16
(clojure.string/split s re)
17
(clojure.string/split s re limit)
18
;; Splits string on regular expression, optionally limiting splits
19
;; Returns: vector of strings
20
21
(clojure.string/split-lines s)
22
;; Splits s on \n or \r\n
23
;; Returns: vector of strings
24
25
(clojure.string/replace s match replacement)
26
;; Replaces all instances of match with replacement in s
27
;; match can be string, character, or regex pattern
28
;; replacement can be string or function
29
;; Returns: String
30
31
(clojure.string/replace-first s match replacement)
32
;; Replaces first instance of match with replacement in s
33
;; Returns: String
34
35
(clojure.string/reverse s)
36
;; Returns s with characters in reverse order
37
;; Returns: String
38
```
39
40
### Case Conversion
41
42
Functions for changing string case.
43
44
```clojure { .api }
45
(clojure.string/upper-case s)
46
;; Converts string to all upper-case
47
;; Returns: String
48
49
(clojure.string/lower-case s)
50
;; Converts string to all lower-case
51
;; Returns: String
52
53
(clojure.string/capitalize s)
54
;; Converts first character to upper-case, rest to lower-case
55
;; Returns: String
56
```
57
58
### Whitespace Handling
59
60
Functions for trimming and managing whitespace.
61
62
```clojure { .api }
63
(clojure.string/trim s)
64
;; Removes whitespace from both ends of string
65
;; Returns: String
66
67
(clojure.string/triml s)
68
;; Removes whitespace from left side of string
69
;; Returns: String
70
71
(clojure.string/trimr s)
72
;; Removes whitespace from right side of string
73
;; Returns: String
74
75
(clojure.string/trim-newline s)
76
;; Removes all trailing newline \\n or \\r\\n characters
77
;; Returns: String
78
```
79
80
### String Predicates
81
82
Functions for testing string properties.
83
84
```clojure { .api }
85
(clojure.string/blank? s)
86
;; Returns true if s is nil, empty, or contains only whitespace
87
;; Returns: boolean
88
89
(clojure.string/starts-with? s substr)
90
;; Returns true if s starts with substr
91
;; Returns: boolean
92
93
(clojure.string/ends-with? s substr)
94
;; Returns true if s ends with substr
95
;; Returns: boolean
96
97
(clojure.string/includes? s substr)
98
;; Returns true if s contains substr
99
;; Returns: boolean
100
```
101
102
### String Search
103
104
Functions for finding substrings and positions.
105
106
```clojure { .api }
107
(clojure.string/index-of s value)
108
(clojure.string/index-of s value from-index)
109
;; Returns index of first occurrence of value in s, optionally starting from from-index
110
;; Returns nil if not found
111
;; Returns: Long or nil
112
113
(clojure.string/last-index-of s value)
114
(clojure.string/last-index-of s value from-index)
115
;; Returns index of last occurrence of value in s, optionally starting from from-index
116
;; Returns nil if not found
117
;; Returns: Long or nil
118
```
119
120
### String Transformation
121
122
Advanced string processing and transformation functions.
123
124
```clojure { .api }
125
(clojure.string/escape s cmap)
126
;; Returns new string with character mapping applied
127
;; cmap is map of characters to replacement strings
128
;; Returns: String
129
130
(clojure.string/re-quote-replacement s)
131
;; Returns string with regex replacement special characters escaped
132
;; Use when replacement string contains $ or \
133
;; Returns: String
134
```
135
136
### Core String Functions
137
138
Core Clojure functions that work with strings.
139
140
```clojure { .api }
141
(str & xs)
142
;; Returns string representation of arguments concatenated
143
;; Calls toString on non-string arguments
144
;; Returns: String
145
146
(subs s start)
147
(subs s start end)
148
;; Returns substring from start (inclusive) to end (exclusive)
149
;; If no end specified, goes to end of string
150
;; Returns: String
151
152
(count s)
153
;; Returns number of characters in string
154
;; Returns: int
155
156
(nth s index)
157
(nth s index not-found)
158
;; Returns character at index, or not-found if out of bounds
159
;; Returns: Character
160
161
(seq s)
162
;; Returns sequence of characters in string
163
;; Returns: clojure.lang.ISeq or nil if empty
164
165
(char c)
166
;; Coerces c to character
167
;; c can be Character, number, or keyword
168
;; Returns: Character
169
170
(int c)
171
;; Returns integer value of character
172
;; Returns: int
173
```
174
175
### Regular Expressions
176
177
Clojure provides literal syntax and functions for regex processing.
178
179
```clojure { .api }
180
(re-pattern s)
181
;; Compiles string into regex Pattern
182
;; Returns: java.util.regex.Pattern
183
184
(re-matcher pattern s)
185
;; Returns Matcher for pattern and string
186
;; Returns: java.util.regex.Matcher
187
188
(re-find matcher)
189
(re-find pattern s)
190
;; Returns next match from matcher, or first match of pattern in s
191
;; Returns: String or vector [match group1 group2 ...]
192
193
(re-matches pattern s)
194
;; Returns match if pattern matches entire string
195
;; Returns: String or vector [match group1 group2 ...] or nil
196
197
(re-seq pattern s)
198
;; Returns lazy sequence of all matches
199
;; Returns: clojure.lang.LazySeq
200
201
;; Regex literal syntax
202
#"pattern" ; Creates compiled Pattern object
203
#"(?i)pattern" ; Case-insensitive pattern
204
```
205
206
**Usage Examples:**
207
208
```clojure
209
;; Basic operations
210
(clojure.string/join ", " ["apple" "banana" "cherry"]) ; => "apple, banana, cherry"
211
(clojure.string/split "a,b,c" #",") ; => ["a" "b" "c"]
212
(clojure.string/replace "hello world" "world" "there") ; => "hello there"
213
214
;; Case conversion
215
(clojure.string/upper-case "hello") ; => "HELLO"
216
(clojure.string/lower-case "WORLD") ; => "world"
217
(clojure.string/capitalize "hello") ; => "Hello"
218
219
;; Whitespace handling
220
(clojure.string/trim " hello ") ; => "hello"
221
(clojure.string/blank? " ") ; => true
222
(clojure.string/blank? "hello") ; => false
223
224
;; String predicates
225
(clojure.string/starts-with? "hello world" "hello") ; => true
226
(clojure.string/ends-with? "hello world" "world") ; => true
227
(clojure.string/includes? "hello world" "lo wo") ; => true
228
229
;; String search
230
(clojure.string/index-of "hello world" "world") ; => 6
231
(clojure.string/last-index-of "hello hello" "hello") ; => 6
232
233
;; Core string functions
234
(str "hello" " " "world") ; => "hello world"
235
(subs "hello world" 6) ; => "world"
236
(subs "hello world" 0 5) ; => "hello"
237
(count "hello") ; => 5
238
239
;; Character operations
240
(nth "hello" 1) ; => \e
241
(seq "abc") ; => (\a \b \c)
242
(char 65) ; => \A
243
(int \A) ; => 65
244
245
;; Regular expressions
246
(re-find #"\d+" "abc123def") ; => "123"
247
(re-seq #"\w+" "hello world") ; => ("hello" "world")
248
(re-matches #"\d{3}-\d{4}" "123-4567") ; => "123-4567"
249
250
;; Advanced replacements
251
(clojure.string/replace "hello world" #"\w+" str/upper-case) ; => "HELLO WORLD"
252
(clojure.string/escape "a<b>c" {\< "<" \> ">"}) ; => "a<b>c"
253
```