0
# String Processing
1
2
String manipulation functions including splitting, joining, case conversion, and character-level operations. The String module provides 13 core functions plus inherits additional functions from the List module for treating strings as character arrays.
3
4
## Capabilities
5
6
### String Splitting and Joining
7
8
Functions for breaking strings apart and combining arrays into strings.
9
10
```javascript { .api }
11
/**
12
* Splits string by separator
13
* @param {string} separator - String or regex to split by
14
* @param {string} string - String to split
15
* @returns {Array<string>} Array of split parts
16
*/
17
function split(separator, string);
18
19
/**
20
* Joins array elements with separator
21
* @param {string} separator - String to join with
22
* @param {Array} array - Array of elements to join
23
* @returns {string} Joined string
24
*/
25
function join(separator, array);
26
27
/**
28
* Splits string into lines
29
* @param {string} string - String to split
30
* @returns {Array<string>} Array of lines
31
*/
32
function lines(string);
33
34
/**
35
* Joins lines with newline characters
36
* @param {Array<string>} lines - Array of line strings
37
* @returns {string} Multi-line string
38
*/
39
function unlines(lines);
40
41
/**
42
* Splits string into words (by whitespace)
43
* @param {string} string - String to split
44
* @returns {Array<string>} Array of words
45
*/
46
function words(string);
47
48
/**
49
* Joins words with space characters
50
* @param {Array<string>} words - Array of word strings
51
* @returns {string} Space-separated string
52
*/
53
function unwords(words);
54
55
/**
56
* Splits string into character array
57
* @param {string} string - String to split
58
* @returns {Array<string>} Array of individual characters
59
*/
60
function chars(string);
61
62
/**
63
* Joins character array into string
64
* @param {Array<string>} chars - Array of single characters
65
* @returns {string} Combined string
66
*/
67
function unchars(chars);
68
```
69
70
### String Manipulation
71
72
Functions for transforming strings.
73
74
```javascript { .api }
75
/**
76
* Reverses string
77
* @param {string} string - String to reverse
78
* @returns {string} Reversed string
79
*/
80
function reverse(string);
81
82
/**
83
* Repeats string n times
84
* @param {number} n - Number of repetitions
85
* @param {string} string - String to repeat
86
* @returns {string} Repeated string
87
*/
88
function repeat(n, string);
89
90
/**
91
* Capitalizes first letter of string
92
* @param {string} string - String to capitalize
93
* @returns {string} String with first letter capitalized
94
*/
95
function capitalize(string);
96
97
/**
98
* Converts string to camelCase
99
* @param {string} string - String to convert
100
* @returns {string} camelCase string
101
*/
102
function camelize(string);
103
104
/**
105
* Converts string to dash-case (kebab-case)
106
* @param {string} string - String to convert
107
* @returns {string} dash-case string
108
*/
109
function dasherize(string);
110
```
111
112
### Inherited List Operations
113
114
String module inherits these functions from List module for character-level string operations:
115
116
```javascript { .api }
117
/**
118
* Checks if string is empty
119
* @param {string} string - String to check
120
* @returns {boolean} True if string has no characters
121
*/
122
function empty(string);
123
124
/**
125
* Extracts substring (like Array.slice)
126
* @param {number} start - Start index (inclusive)
127
* @param {number} end - End index (exclusive)
128
* @param {string} string - String to slice
129
* @returns {string} Substring
130
*/
131
function slice(start, end, string);
132
133
/**
134
* Takes first n characters
135
* @param {number} n - Number of characters to take
136
* @param {string} string - String to take from
137
* @returns {string} First n characters
138
*/
139
function take(n, string);
140
141
/**
142
* Drops first n characters
143
* @param {number} n - Number of characters to drop
144
* @param {string} string - String to drop from
145
* @returns {string} String without first n characters
146
*/
147
function drop(n, string);
148
149
/**
150
* Splits string at index into two strings
151
* @param {number} index - Index to split at
152
* @param {string} string - String to split
153
* @returns {Array<string>} [firstPart, secondPart]
154
*/
155
function splitAt(index, string);
156
157
/**
158
* Takes characters while predicate is true
159
* @param {Function} predicate - Function testing characters
160
* @param {string} string - String to take from
161
* @returns {string} Characters taken while predicate true
162
*/
163
function takeWhile(predicate, string);
164
165
/**
166
* Drops characters while predicate is true
167
* @param {Function} predicate - Function testing characters
168
* @param {string} string - String to drop from
169
* @returns {string} Remaining characters after dropping
170
*/
171
function dropWhile(predicate, string);
172
173
/**
174
* Splits into [takeWhile, dropWhile] results
175
* @param {Function} predicate - Function testing characters
176
* @param {string} string - String to split
177
* @returns {Array<string>} [taken, remaining]
178
*/
179
function span(predicate, string);
180
181
/**
182
* Breaks string where predicate first becomes true
183
* @param {Function} predicate - Function testing characters
184
* @param {string} string - String to break
185
* @returns {Array<string>} [before, after] break point
186
*/
187
function breakStr(predicate, string);
188
```
189
190
## Usage Examples
191
192
**Basic String Operations:**
193
194
```javascript
195
const { split, join, words, unwords } = require('prelude-ls');
196
197
const sentence = 'Hello world from prelude';
198
const wordArray = words(sentence); // ['Hello', 'world', 'from', 'prelude']
199
const rejoined = unwords(wordArray); // 'Hello world from prelude'
200
201
const csvData = 'apple,banana,cherry';
202
const fruits = split(',', csvData); // ['apple', 'banana', 'cherry']
203
const backToCsv = join(',', fruits); // 'apple,banana,cherry'
204
```
205
206
**Case Conversion:**
207
208
```javascript
209
const { capitalize, camelize, dasherize } = require('prelude-ls');
210
211
const text = 'hello world';
212
const capitalized = capitalize(text); // 'Hello world'
213
214
const varName = 'user_first_name';
215
const camelCase = camelize(varName); // 'userFirstName'
216
const dashCase = dasherize(varName); // 'user-first-name'
217
```
218
219
**Character-Level Operations:**
220
221
```javascript
222
const { chars, unchars, take, drop } = require('prelude-ls');
223
224
const word = 'prelude';
225
const letters = chars(word); // ['p', 'r', 'e', 'l', 'u', 'd', 'e']
226
const backToWord = unchars(letters); // 'prelude'
227
228
const firstThree = take(3, word); // 'pre'
229
const withoutFirst = drop(1, word); // 'relude'
230
```
231
232
**String Repetition and Reversal:**
233
234
```javascript
235
const { repeat, reverse } = require('prelude-ls');
236
237
const pattern = repeat(3, 'abc'); // 'abcabcabc'
238
const backwards = reverse('hello'); // 'olleh'
239
```
240
241
**Multi-line Text Processing:**
242
243
```javascript
244
const { lines, unlines, words, map, filter } = require('prelude-ls');
245
246
const text = `Line one
247
Line two
248
Line three`;
249
250
const lineArray = lines(text); // ['Line one', 'Line two', 'Line three']
251
const processedLines = map(line => `* ${line}`, lineArray);
252
const bulleted = unlines(processedLines);
253
// `* Line one
254
// * Line two
255
// * Line three`
256
```
257
258
**Curried String Processing:**
259
260
```javascript
261
const { split, map, join, filter } = require('prelude-ls');
262
263
// Create reusable string processors
264
const splitByComma = split(',');
265
const joinWithPipe = join('|');
266
const nonEmpty = filter(s => s.length > 0);
267
268
const csvData = 'apple,,banana,cherry,';
269
const cleaned = joinWithPipe(nonEmpty(splitByComma(csvData)));
270
// Result: 'apple|banana|cherry'
271
```
272
273
**Predicate-Based String Operations:**
274
275
```javascript
276
const { takeWhile, dropWhile, span } = require('prelude-ls');
277
278
const text = 'aaabbbccc';
279
const isA = char => char === 'a';
280
281
const leadingAs = takeWhile(isA, text); // 'aaa'
282
const withoutAs = dropWhile(isA, text); // 'bbbccc'
283
const [as, rest] = span(isA, text); // ['aaa', 'bbbccc']
284
```
285
286
**Text Analysis:**
287
288
```javascript
289
const { chars, filter, length } = require('prelude-ls');
290
291
const sentence = 'Hello, World!';
292
const letters = filter(char => /[a-zA-Z]/.test(char), chars(sentence));
293
const letterCount = letters.length; // 10
294
295
const vowels = filter(char => 'aeiouAEIOU'.includes(char), chars(sentence));
296
const vowelCount = vowels.length; // 3
297
```
298
299
**Complex Text Transformation:**
300
301
```javascript
302
const { words, map, filter, unwords, capitalize } = require('prelude-ls');
303
304
const text = 'hello world from javascript and livescript';
305
const processed = unwords(
306
map(capitalize,
307
filter(word => word.length > 4,
308
words(text))));
309
// Result: 'Hello World Javascript Livescript'
310
```