1Stdlib.String(3) OCaml library Stdlib.String(3)
2
3
4
6 Stdlib.String - no description
7
9 Module Stdlib.String
10
12 Module String
13 : (module Stdlib__string)
14
15
16
17
18
19
20
21
22
23 Strings
24 type t = string
25
26
27 The type for strings.
28
29
30
31 val make : int -> char -> string
32
33
34 make n c is a string of length n with each index holding the character
35 c .
36
37
38 Raises Invalid_argument if n < 0 or n > Sys.max_string_length .
39
40
41
42 val init : int -> (int -> char) -> string
43
44
45 init n f is a string of length n with index i holding the character f i
46 (called in increasing index order).
47
48
49 Since 4.02.0
50
51
52 Raises Invalid_argument if n < 0 or n > Sys.max_string_length .
53
54
55
56 val length : string -> int
57
58
59 length s is the length (number of bytes/characters) of s .
60
61
62
63 val get : string -> int -> char
64
65
66 get s i is the character at index i in s . This is the same as writing
67 s.[i] .
68
69
70 Raises Invalid_argument if i not an index of s .
71
72
73
74
75 Concatenating
76 Note. The (^) binary operator concatenates two strings.
77
78 val concat : string -> string list -> string
79
80
81 concat sep ss concatenates the list of strings ss , inserting the sepa‐
82 rator string sep between each.
83
84
85 Raises Invalid_argument if the result is longer than
86 Sys.max_string_length bytes.
87
88
89
90
91 Predicates and comparisons
92 val equal : t -> t -> bool
93
94
95 equal s0 s1 is true if and only if s0 and s1 are character-wise equal.
96
97
98 Since 4.03.0 (4.05.0 in StringLabels)
99
100
101
102 val compare : t -> t -> int
103
104
105 compare s0 s1 sorts s0 and s1 in lexicographical order. compare be‐
106 haves like compare on strings but may be more efficient.
107
108
109
110 val contains_from : string -> int -> char -> bool
111
112
113 contains_from s start c is true if and only if c appears in s after po‐
114 sition start .
115
116
117 Raises Invalid_argument if start is not a valid position in s .
118
119
120
121 val rcontains_from : string -> int -> char -> bool
122
123
124 rcontains_from s stop c is true if and only if c appears in s before
125 position stop+1 .
126
127
128 Raises Invalid_argument if stop < 0 or stop+1 is not a valid position
129 in s .
130
131
132
133 val contains : string -> char -> bool
134
135
136 contains s c is String.contains_from s 0 c .
137
138
139
140
141 Extracting substrings
142 val sub : string -> int -> int -> string
143
144
145 sub s pos len is a string of length len , containing the substring of s
146 that starts at position pos and has length len .
147
148
149 Raises Invalid_argument if pos and len do not designate a valid sub‐
150 string of s .
151
152
153
154 val split_on_char : char -> string -> string list
155
156
157 split_on_char sep s is the list of all (possibly empty) substrings of s
158 that are delimited by the character sep .
159
160 The function's result is specified by the following invariants:
161
162 -The list is not empty.
163
164 -Concatenating its elements using sep as a separator returns a string
165 equal to the input ( concat (make 1 sep)
166 (split_on_char sep s) = s ).
167
168 -No string in the result contains the sep character.
169
170
171
172 Since 4.04.0 (4.05.0 in StringLabels)
173
174
175
176
177 Transforming
178 val map : (char -> char) -> string -> string
179
180
181 map f s is the string resulting from applying f to all the characters
182 of s in increasing order.
183
184
185 Since 4.00.0
186
187
188
189 val mapi : (int -> char -> char) -> string -> string
190
191
192 mapi f s is like String.map but the index of the character is also
193 passed to f .
194
195
196 Since 4.02.0
197
198
199
200 val trim : string -> string
201
202
203 trim s is s without leading and trailing whitespace. Whitespace charac‐
204 ters are: ' ' , '\x0C' (form feed), '\n' , '\r' , and '\t' .
205
206
207 Since 4.00.0
208
209
210
211 val escaped : string -> string
212
213
214 escaped s is s with special characters represented by escape sequences,
215 following the lexical conventions of OCaml.
216
217 All characters outside the US-ASCII printable range [0x20;0x7E] are es‐
218 caped, as well as backslash (0x2F) and double-quote (0x22).
219
220 The function Scanf.unescaped is a left inverse of escaped , i.e.
221 Scanf.unescaped (escaped s) = s for any string s (unless escaped s
222 fails).
223
224
225 Raises Invalid_argument if the result is longer than
226 Sys.max_string_length bytes.
227
228
229
230 val uppercase_ascii : string -> string
231
232
233 uppercase_ascii s is s with all lowercase letters translated to upper‐
234 case, using the US-ASCII character set.
235
236
237 Since 4.03.0 (4.05.0 in StringLabels)
238
239
240
241 val lowercase_ascii : string -> string
242
243
244 lowercase_ascii s is s with all uppercase letters translated to lower‐
245 case, using the US-ASCII character set.
246
247
248 Since 4.03.0 (4.05.0 in StringLabels)
249
250
251
252 val capitalize_ascii : string -> string
253
254
255 capitalize_ascii s is s with the first character set to uppercase, us‐
256 ing the US-ASCII character set.
257
258
259 Since 4.03.0 (4.05.0 in StringLabels)
260
261
262
263 val uncapitalize_ascii : string -> string
264
265
266 uncapitalize_ascii s is s with the first character set to lowercase,
267 using the US-ASCII character set.
268
269
270 Since 4.03.0 (4.05.0 in StringLabels)
271
272
273
274
275 Traversing
276 val iter : (char -> unit) -> string -> unit
277
278
279 iter f s applies function f in turn to all the characters of s . It is
280 equivalent to f s.[0]; f s.[1]; ...; f s.[length s - 1]; () .
281
282
283
284 val iteri : (int -> char -> unit) -> string -> unit
285
286
287 iteri is like String.iter , but the function is also given the corre‐
288 sponding character index.
289
290
291 Since 4.00.0
292
293
294
295
296 Searching
297 val index_from : string -> int -> char -> int
298
299
300 index_from s i c is the index of the first occurrence of c in s after
301 position i .
302
303
304 Raises Not_found if c does not occur in s after position i .
305
306
307 Raises Invalid_argument if i is not a valid position in s .
308
309
310
311 val index_from_opt : string -> int -> char -> int option
312
313
314 index_from_opt s i c is the index of the first occurrence of c in s af‐
315 ter position i (if any).
316
317
318 Since 4.05
319
320
321 Raises Invalid_argument if i is not a valid position in s .
322
323
324
325 val rindex_from : string -> int -> char -> int
326
327
328 rindex_from s i c is the index of the last occurrence of c in s before
329 position i+1 .
330
331
332 Raises Not_found if c does not occur in s before position i+1 .
333
334
335 Raises Invalid_argument if i+1 is not a valid position in s .
336
337
338
339 val rindex_from_opt : string -> int -> char -> int option
340
341
342 rindex_from_opt s i c is the index of the last occurrence of c in s be‐
343 fore position i+1 (if any).
344
345
346 Since 4.05
347
348
349 Raises Invalid_argument if i+1 is not a valid position in s .
350
351
352
353 val index : string -> char -> int
354
355
356 index s c is String.index_from s 0 c .
357
358
359
360 val index_opt : string -> char -> int option
361
362
363 index_opt s c is String.index_from_opt s 0 c .
364
365
366 Since 4.05
367
368
369
370 val rindex : string -> char -> int
371
372
373 rindex s c is String.rindex_from s (length s - 1) c .
374
375
376
377 val rindex_opt : string -> char -> int option
378
379
380 rindex_opt s c is String.rindex_from_opt s (length s - 1) c .
381
382
383 Since 4.05
384
385
386
387
388 Converting
389 val to_seq : t -> char Seq.t
390
391
392 to_seq s is a sequence made of the string's characters in increasing
393 order. In "unsafe-string" mode, modifications of the string during it‐
394 eration will be reflected in the iterator.
395
396
397 Since 4.07
398
399
400
401 val to_seqi : t -> (int * char) Seq.t
402
403
404 to_seqi s is like String.to_seq but also tuples the corresponding in‐
405 dex.
406
407
408 Since 4.07
409
410
411
412 val of_seq : char Seq.t -> t
413
414
415 of_seq s is a string made of the sequence's characters.
416
417
418 Since 4.07
419
420
421
422
423 Deprecated functions
424 val create : int -> bytes
425
426 Deprecated. This is a deprecated alias of Bytes.create / BytesLa‐
427 bels.create .
428
429
430
431 create n returns a fresh byte sequence of length n . The sequence is
432 uninitialized and contains arbitrary bytes.
433
434
435 Raises Invalid_argument if n < 0 or n > Sys.max_string_length .
436
437
438
439 val set : bytes -> int -> char -> unit
440
441 Deprecated. This is a deprecated alias of Bytes.set / BytesLabels.set
442 .
443
444
445
446 set s n c modifies byte sequence s in place, replacing the byte at in‐
447 dex n with c . You can also write s.[n] <- c instead of set s n c .
448
449
450 Raises Invalid_argument if n is not a valid index in s .
451
452
453
454 val blit : string -> int -> bytes -> int -> int -> unit
455
456
457 blit src src_pos dst dst_pos len copies len bytes from the string src ,
458 starting at index src_pos , to byte sequence dst , starting at charac‐
459 ter number dst_pos .
460
461
462 Raises Invalid_argument if src_pos and len do not designate a valid
463 range of src , or if dst_pos and len do not designate a valid range of
464 dst .
465
466
467
468 val copy : string -> string
469
470 Deprecated. Because strings are immutable, it doesn't make much sense
471 to make identical copies of them.
472
473
474 Return a copy of the given string.
475
476
477
478 val fill : bytes -> int -> int -> char -> unit
479
480 Deprecated. This is a deprecated alias of Bytes.fill / BytesLa‐
481 bels.fill .
482
483
484
485 fill s pos len c modifies byte sequence s in place, replacing len bytes
486 by c , starting at pos .
487
488
489 Raises Invalid_argument if pos and len do not designate a valid sub‐
490 string of s .
491
492
493
494 val uppercase : string -> string
495
496 Deprecated. Functions operating on Latin-1 character set are depre‐
497 cated.
498
499
500 Return a copy of the argument, with all lowercase letters translated to
501 uppercase, including accented letters of the ISO Latin-1 (8859-1) char‐
502 acter set.
503
504
505
506 val lowercase : string -> string
507
508 Deprecated. Functions operating on Latin-1 character set are depre‐
509 cated.
510
511
512 Return a copy of the argument, with all uppercase letters translated to
513 lowercase, including accented letters of the ISO Latin-1 (8859-1) char‐
514 acter set.
515
516
517
518 val capitalize : string -> string
519
520 Deprecated. Functions operating on Latin-1 character set are depre‐
521 cated.
522
523
524 Return a copy of the argument, with the first character set to upper‐
525 case, using the ISO Latin-1 (8859-1) character set..
526
527
528
529 val uncapitalize : string -> string
530
531 Deprecated. Functions operating on Latin-1 character set are depre‐
532 cated.
533
534
535 Return a copy of the argument, with the first character set to lower‐
536 case, using the ISO Latin-1 (8859-1) character set.
537
538
539
540
541
542OCamldoc 2021-07-22 Stdlib.String(3)