Module Import0.StringLabels
String operations.
val get : string -> int -> char
String.get s n
returns the character at indexn
in strings
. You can also writes.[n]
instead ofString.get s n
.Raise
Invalid_argument
ifn
not a valid index ins
.
val set : bytes -> int -> char -> unit
String.set s n c
modifies byte sequences
in place, replacing the byte at indexn
withc
. You can also writes.[n] <- c
instead ofString.set s n c
.Raise
Invalid_argument
ifn
is not a valid index ins
.- deprecated
This is a deprecated alias of
BytesLabels
.set.
val create : int -> bytes
String.create n
returns a fresh byte sequence of lengthn
. The sequence is uninitialized and contains arbitrary bytes.Raise
Invalid_argument
ifn < 0
orn >
Sys.max_string_length
.- deprecated
This is a deprecated alias of
BytesLabels
.create.
val make : int -> char -> string
String.make n c
returns a fresh string of lengthn
, filled with the characterc
.Raise
Invalid_argument
ifn < 0
orn >
Sys.max_string_length
.
val init : int -> f:(int -> char) -> string
init n f
returns a string of lengthn
, with characteri
initialized to the result off i
.Raise
Invalid_argument
ifn < 0
orn >
Sys.max_string_length
.- since
- 4.02.0
val sub : string -> pos:int -> len:int -> string
String.sub s start len
returns a fresh string of lengthlen
, containing the substring ofs
that starts at positionstart
and has lengthlen
.Raise
Invalid_argument
ifstart
andlen
do not designate a valid substring ofs
.
val fill : bytes -> pos:int -> len:int -> char -> unit
String.fill s start len c
modifies byte sequences
in place, replacinglen
bytes byc
, starting atstart
.Raise
Invalid_argument
ifstart
andlen
do not designate a valid substring ofs
.- deprecated
This is a deprecated alias of
BytesLabels
.fill.
val blit : src:string -> src_pos:int -> dst:bytes -> dst_pos:int -> len:int -> unit
String.blit src srcoff dst dstoff len
copieslen
bytes from the stringsrc
, starting at indexsrcoff
, to byte sequencedst
, starting at character numberdstoff
.Raise
Invalid_argument
ifsrcoff
andlen
do not designate a valid range ofsrc
, or ifdstoff
andlen
do not designate a valid range ofdst
.
val concat : sep:string -> string list -> string
String.concat sep sl
concatenates the list of stringssl
, inserting the separator stringsep
between each.
val iter : f:(char -> unit) -> string -> unit
String.iter f s
applies functionf
in turn to all the characters ofs
. It is equivalent tof s.[0]; f s.[1]; ...; f s.[String.length s - 1]; ()
.
val iteri : f:(int -> char -> unit) -> string -> unit
Same as
String.iter
, but the function is applied to the index of the element as first argument (counting from 0), and the character itself as second argument.- since
- 4.00.0
val map : f:(char -> char) -> string -> string
String.map f s
applies functionf
in turn to all the characters ofs
and stores the results in a new string that is returned.- since
- 4.00.0
val mapi : f:(int -> char -> char) -> string -> string
String.mapi f s
callsf
with each character ofs
and its index (in increasing index order) and stores the results in a new string that is returned.- since
- 4.02.0
val trim : string -> string
Return a copy of the argument, without leading and trailing whitespace. The characters regarded as whitespace are:
' '
,'\012'
,'\n'
,'\r'
, and'\t'
. If there is no leading nor trailing whitespace character in the argument, return the original string itself, not a copy.- since
- 4.00.0
val escaped : string -> string
Return a copy of the argument, with special characters represented by escape sequences, following the lexical conventions of OCaml. If there is no special character in the argument, return the original string itself, not a copy. Its inverse function is Scanf.unescaped.
val index : string -> char -> int
String.index s c
returns the index of the first occurrence of characterc
in strings
.Raise
Not_found
ifc
does not occur ins
.
val index_opt : string -> char -> int option
String.index_opt s c
returns the index of the first occurrence of characterc
in strings
, orNone
ifc
does not occur ins
.- since
- 4.05
val rindex : string -> char -> int
String.rindex s c
returns the index of the last occurrence of characterc
in strings
.Raise
Not_found
ifc
does not occur ins
.
val rindex_opt : string -> char -> int option
String.rindex_opt s c
returns the index of the last occurrence of characterc
in strings
, orNone
ifc
does not occur ins
.- since
- 4.05
val index_from : string -> int -> char -> int
String.index_from s i c
returns the index of the first occurrence of characterc
in strings
after positioni
.String.index s c
is equivalent toString.index_from s 0 c
.Raise
Invalid_argument
ifi
is not a valid position ins
. RaiseNot_found
ifc
does not occur ins
after positioni
.
val index_from_opt : string -> int -> char -> int option
String.index_from_opt s i c
returns the index of the first occurrence of characterc
in strings
after positioni
orNone
ifc
does not occur ins
after positioni
.String.index_opt s c
is equivalent toString.index_from_opt s 0 c
. RaiseInvalid_argument
ifi
is not a valid position ins
.- since
- 4.05
val rindex_from : string -> int -> char -> int
String.rindex_from s i c
returns the index of the last occurrence of characterc
in strings
before positioni+1
.String.rindex s c
is equivalent toString.rindex_from s (String.length s - 1) c
.Raise
Invalid_argument
ifi+1
is not a valid position ins
. RaiseNot_found
ifc
does not occur ins
before positioni+1
.
val rindex_from_opt : string -> int -> char -> int option
String.rindex_from_opt s i c
returns the index of the last occurrence of characterc
in strings
before positioni+1
orNone
ifc
does not occur ins
before positioni+1
.String.rindex_opt s c
is equivalent toString.rindex_from_opt s (String.length s - 1) c
.Raise
Invalid_argument
ifi+1
is not a valid position ins
.- since
- 4.05
val contains : string -> char -> bool
String.contains s c
tests if characterc
appears in the strings
.
val contains_from : string -> int -> char -> bool
String.contains_from s start c
tests if characterc
appears ins
after positionstart
.String.contains s c
is equivalent toString.contains_from s 0 c
.Raise
Invalid_argument
ifstart
is not a valid position ins
.
val rcontains_from : string -> int -> char -> bool
String.rcontains_from s stop c
tests if characterc
appears ins
before positionstop+1
.Raise
Invalid_argument
ifstop < 0
orstop+1
is not a valid position ins
.
val uppercase : string -> string
Return a copy of the argument, with all lowercase letters translated to uppercase, including accented letters of the ISO Latin-1 (8859-1) character set.
- deprecated
Functions operating on Latin-1 character set are deprecated.
val lowercase : string -> string
Return a copy of the argument, with all uppercase letters translated to lowercase, including accented letters of the ISO Latin-1 (8859-1) character set.
- deprecated
Functions operating on Latin-1 character set are deprecated.
val capitalize : string -> string
Return a copy of the argument, with the first character set to uppercase, using the ISO Latin-1 (8859-1) character set..
- deprecated
Functions operating on Latin-1 character set are deprecated.
val uncapitalize : string -> string
Return a copy of the argument, with the first character set to lowercase, using the ISO Latin-1 (8859-1) character set..
- deprecated
Functions operating on Latin-1 character set are deprecated.
val uppercase_ascii : string -> string
Return a copy of the argument, with all lowercase letters translated to uppercase, using the US-ASCII character set.
- since
- 4.05.0
val lowercase_ascii : string -> string
Return a copy of the argument, with all uppercase letters translated to lowercase, using the US-ASCII character set.
- since
- 4.05.0
val capitalize_ascii : string -> string
Return a copy of the argument, with the first character set to uppercase, using the US-ASCII character set.
- since
- 4.05.0
val uncapitalize_ascii : string -> string
Return a copy of the argument, with the first character set to lowercase, using the US-ASCII character set.
- since
- 4.05.0
val compare : t -> t -> int
The comparison function for strings, with the same specification as
Stdlib.compare
. Along with the typet
, this functioncompare
allows the moduleString
to be passed as argument to the functorsSet
.Make andMap
.Make.
val split_on_char : sep:char -> string -> string list
String.split_on_char sep s
returns the list of all (possibly empty) substrings ofs
that are delimited by thesep
character.The function's output is specified by the following invariants:
- The list is not empty.
- Concatenating its elements using
sep
as a separator returns a string equal to the input (String.concat (String.make 1 sep) (String.split_on_char sep s) = s
). - No string in the result contains the
sep
character.
- since
- 4.05.0
Iterators
val to_seq : t -> char Seq.t
Iterate on the string, in increasing index order. Modifications of the string during iteration will be reflected in the iterator.
- since
- 4.07