Module String.Prototype

String.prototype methods

Length

val length : string -> int

length s returns the UTF-16 length of string s (number of code units). Note: This differs from OCaml's String.length which returns byte count.

Case conversion

val to_lower_case : string -> string

to_lower_case s returns a new string with all characters converted to lowercase, including context-sensitive mappings such as the Greek final sigma. Equivalent to JavaScript's String.prototype.toLowerCase().

val to_upper_case : string -> string

to_upper_case s returns a new string with all characters converted to uppercase. Equivalent to JavaScript's String.prototype.toUpperCase().

val normalize : normalization -> string -> string option

normalize form s returns the Unicode Normalization Form of string s. Returns None if normalization fails. Equivalent to JavaScript's String.prototype.normalize().

Character access

val char_at : int -> string -> string

char_at idx s returns the character at UTF-16 index idx as a string. Returns empty string if index is out of bounds. Equivalent to JavaScript's String.prototype.charAt().

val char_code_at : int -> string -> int option

char_code_at idx s returns the UTF-16 code unit at index idx. Returns None if index is out of bounds. Equivalent to JavaScript's String.prototype.charCodeAt().

val code_point_at : int -> string -> int option

code_point_at idx s returns the full Unicode code point at UTF-16 index idx. For surrogate pairs, returns the full code point when idx points to the high surrogate. Returns None if index is out of bounds. Equivalent to JavaScript's String.prototype.codePointAt().

Substring extraction

val slice : start:int -> end_:int -> string -> string

slice ~start ~end_ s extracts a section of string. Negative indices count from the end. Equivalent to JavaScript's String.prototype.slice().

val slice_from : int -> string -> string

slice_from start s extracts from start to end of string.

val substring : start:int -> end_:int -> string -> string

substring ~start ~end_ s extracts characters between two indices. Swaps arguments if start > end. Negative values treated as 0. Equivalent to JavaScript's String.prototype.substring().

val substring_from : int -> string -> string

substring_from start s extracts from start to end of string.

val substr : start:int -> length:int -> string -> string

substr ~start ~length s extracts length characters starting from start. Negative start counts from end. Legacy method (Annex B). Equivalent to JavaScript's String.prototype.substr().

val substr_from : int -> string -> string

substr_from start s extracts from start to end of string.

Search methods

val index_of : string -> string -> int

index_of search s returns UTF-16 index of first occurrence of search in s. Returns -1 if not found (JavaScript semantics). Equivalent to JavaScript's String.prototype.indexOf().

val index_of_from : string -> int -> string -> int

index_of_from search pos s searches starting from position pos.

val last_index_of : string -> string -> int

last_index_of search s returns UTF-16 index of last occurrence of search in s. Returns -1 if not found. Equivalent to JavaScript's String.prototype.lastIndexOf().

val last_index_of_from : string -> int -> string -> int

last_index_of_from search pos s searches backwards starting from position pos.

val includes : string -> string -> bool

includes search s returns true if s contains search. Equivalent to JavaScript's String.prototype.includes().

val includes_from : string -> int -> string -> bool

includes_from search pos s checks from position pos.

val starts_with : string -> string -> bool

starts_with search s returns true if s starts with search. Equivalent to JavaScript's String.prototype.startsWith().

val starts_with_from : string -> int -> string -> bool

starts_with_from search pos s checks if s starts with search at position pos. Out-of-range positions are clamped.

val ends_with : string -> string -> bool

ends_with search s returns true if s ends with search. Equivalent to JavaScript's String.prototype.endsWith().

val ends_with_at : string -> int -> string -> bool

ends_with_at search end_pos s checks if s (up to end_pos) ends with search.

Transform methods

val trim : string -> string

trim s removes leading and trailing JavaScript whitespace (the ECMA-262 WhiteSpace and LineTerminator sets). Equivalent to JavaScript's String.prototype.trim().

val trim_start : string -> string

trim_start s removes leading whitespace. Equivalent to JavaScript's String.prototype.trimStart().

val trim_end : string -> string

trim_end s removes trailing whitespace. Equivalent to JavaScript's String.prototype.trimEnd().

val pad_start : int -> string -> string

pad_start target_len s pads s at the start with spaces to reach target_len. Equivalent to JavaScript's String.prototype.padStart().

val pad_start_with : int -> string -> string -> string

pad_start_with target_len fill_str s pads s at the start with fill_str.

val pad_end : int -> string -> string

pad_end target_len s pads s at the end with spaces to reach target_len. Equivalent to JavaScript's String.prototype.padEnd().

val pad_end_with : int -> string -> string -> string

pad_end_with target_len fill_str s pads s at the end with fill_str.

val repeat : int -> string -> string

repeat n s returns s repeated n times.

  • raises Invalid_argument

    if n is negative. Equivalent to JavaScript's String.prototype.repeat().

RegExp-based methods

All methods in this section compile their pattern argument and raise Invalid_argument when it is not a valid regular expression, like the SyntaxError JavaScript throws.

val match_ : string -> string -> RegExp.match_result option

match_ pattern s returns the first match of pattern in s, with captures, named groups and index, or None if there is no match. Equivalent to JavaScript's String.prototype.match() without the global flag.

val match_flags : string -> string -> string -> RegExp.match_result option

match_flags pattern flags s matches with the given regex flags. The 'g' flag is rejected because a single match_result cannot represent multiple matches; use match_global or match_all instead.

val match_global : ?flags:string -> string -> string -> string array

match_global ?flags pattern s returns all full matches, like JavaScript's String.prototype.match() with the global flag (which returns full matches only, without capture groups). Returns [||] when there is no match (JavaScript returns null). The global flag is added automatically; ?flags can supply extra flags such as "i".

val match_all : ?flags:string -> string -> string -> RegExp.match_result list

match_all ?flags pattern s returns all match results, with captures, named groups and indices. Equivalent to JavaScript's String.prototype.matchAll(). The global flag is added automatically.

search pattern s returns UTF-16 index of first match, or -1 if not found (JavaScript semantics). Equivalent to JavaScript's String.prototype.search().

val search_flags : string -> string -> string -> int

search_flags pattern flags s searches with specified regex flags.

val replace : string -> string -> string -> string

replace search replacement s replaces first occurrence of search with replacement. Equivalent to JavaScript's String.prototype.replace() with string argument. Supports the $$, $&, $` and $' replacement patterns; $` and $' always refer to the original string.

val replace_regex : string -> string -> string -> string

replace_regex pattern replacement s replaces the first regex match. Capture groups are available as $1..$99 in replacement; groups that did not participate substitute the empty string.

val replace_regex_global : string -> string -> string -> string

replace_regex_global pattern replacement s replaces all regex matches.

val replace_regex_flags : string -> string -> string -> string -> string

replace_regex_flags pattern flags replacement s replaces with specified flags.

val replace_all : string -> string -> string -> string

replace_all search replacement s replaces all occurrences. Equivalent to JavaScript's String.prototype.replaceAll() with a string pattern.

val replace_all_regex : string -> string -> string -> string

replace_all_regex pattern replacement s replaces all regex matches.

val split : string -> string -> string array

split separator s splits s by separator. Equivalent to JavaScript's String.prototype.split().

val split_limit : string -> int -> string -> string array

split_limit separator limit s splits into at most limit parts. Like JavaScript, limit is coerced with ToUint32: negative values wrap to huge positive values and behave as "no limit".

val split_regex : string -> string -> string option array

split_regex pattern s splits by regex pattern. Capture groups are spliced into the result, as in JavaScript; a group that did not participate contributes None (JavaScript's undefined), which is distinct from a group that matched the empty string (Some ""). Substrings of s are always Some.

val split_regex_limit : string -> int -> string -> string option array

split_regex_limit pattern limit s splits into at most limit entries. Spliced capture groups count toward the limit and limit is coerced with ToUint32, like JavaScript's split(regexp, limit).