String.PrototypeString.prototype methods
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.
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().
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 optionnormalize form s returns the Unicode Normalization Form of string s. Returns None if normalization fails. Equivalent to JavaScript's String.prototype.normalize().
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().
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().
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().
slice ~start ~end_ s extracts a section of string. Negative indices count from the end. Equivalent to JavaScript's String.prototype.slice().
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().
substring_from start s extracts from start to end of 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().
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().
index_of_from search pos s searches starting from position pos.
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().
last_index_of_from search pos s searches backwards starting from position pos.
includes search s returns true if s contains search. Equivalent to JavaScript's String.prototype.includes().
includes_from search pos s checks from position pos.
starts_with search s returns true if s starts with search. Equivalent to JavaScript's String.prototype.startsWith().
starts_with_from search pos s checks if s starts with search at position pos. Out-of-range positions are clamped.
ends_with search s returns true if s ends with search. Equivalent to JavaScript's String.prototype.endsWith().
ends_with_at search end_pos s checks if s (up to end_pos) ends with search.
trim s removes leading and trailing JavaScript whitespace (the ECMA-262 WhiteSpace and LineTerminator sets). Equivalent to JavaScript's String.prototype.trim().
trim_start s removes leading whitespace. Equivalent to JavaScript's String.prototype.trimStart().
trim_end s removes trailing whitespace. Equivalent to JavaScript's String.prototype.trimEnd().
pad_start target_len s pads s at the start with spaces to reach target_len. Equivalent to JavaScript's String.prototype.padStart().
pad_start_with target_len fill_str s pads s at the start with fill_str.
pad_end target_len s pads s at the end with spaces to reach target_len. Equivalent to JavaScript's String.prototype.padEnd().
pad_end_with target_len fill_str s pads s at the end with fill_str.
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 optionmatch_ 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 optionmatch_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.
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 listmatch_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().
search_flags pattern flags s searches with specified regex flags.
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.
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.
replace_regex_global pattern replacement s replaces all regex matches.
replace_regex_flags pattern flags replacement s replaces with specified flags.
replace_all search replacement s replaces all occurrences. Equivalent to JavaScript's String.prototype.replaceAll() with a string pattern.
replace_all_regex pattern replacement s replaces all regex matches.
split separator s splits s by separator. Equivalent to JavaScript's String.prototype.split().
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".
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.