package quickjs

  1. Overview
  2. Docs

Module String.PrototypeSource

String.prototype methods

Length

Sourceval 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

Sourceval to_lower_case : string -> string

to_lower_case s returns a new string with all characters converted to lowercase. Equivalent to JavaScript's String.prototype.toLowerCase().

Sourceval 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().

Sourceval 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

Sourceval 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().

Sourceval 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().

Sourceval 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

Sourceval 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().

Sourceval slice_from : int -> string -> string

slice_from start s extracts from start to end of string.

Sourceval 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().

Sourceval substring_from : int -> string -> string

substring_from start s extracts from start to end of string.

Sourceval 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().

Sourceval substr_from : int -> string -> string

substr_from start s extracts from start to end of string.

Search methods

Sourceval 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. Equivalent to JavaScript's String.prototype.indexOf().

Sourceval index_of_from : string -> int -> string -> int

index_of_from search pos s searches starting from position pos.

Sourceval 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().

Sourceval last_index_of_from : string -> int -> string -> int

last_index_of_from search pos s searches backwards starting from position pos.

Sourceval includes : string -> string -> bool

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

Sourceval includes_from : string -> int -> string -> bool

includes_from search pos s checks from position pos.

Sourceval starts_with : string -> string -> bool

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

Sourceval starts_with_from : string -> int -> string -> bool

starts_with_from search pos s checks if s starts with search at position pos.

Sourceval ends_with : string -> string -> bool

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

Sourceval 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

Sourceval trim : string -> string

trim s removes leading and trailing whitespace. Equivalent to JavaScript's String.prototype.trim().

Sourceval trim_start : string -> string

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

Sourceval trim_end : string -> string

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

Sourceval 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().

Sourceval pad_start_with : int -> string -> string -> string

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

Sourceval 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().

Sourceval pad_end_with : int -> string -> string -> string

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

Sourceval 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

Sourceval match_ : string -> string -> string array option

match_ pattern s returns captures from first match of pattern in s. Returns None if no match. Equivalent to JavaScript's String.prototype.match().

Sourceval match_flags : string -> string -> string -> string array option

match_flags pattern flags s matches with specified regex flags.

Sourceval match_global : string -> string -> string array

match_global pattern s returns all matches (with global flag).

Sourcetype match_result = {
  1. full_match : string;
  2. captures : string array;
  3. groups : (string * string) list;
  4. index : int;
}

Result type for matchAll

Sourceval match_all : string -> string -> match_result list

match_all pattern s returns iterator-like list of all match results. Equivalent to JavaScript's String.prototype.matchAll().

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

Sourceval search_flags : string -> string -> string -> int

search_flags pattern flags s searches with specified regex flags.

Sourceval 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.

Sourceval replace_regex : string -> string -> string -> string

replace_regex pattern replacement s replaces first regex match.

Sourceval replace_regex_global : string -> string -> string -> string

replace_regex_global pattern replacement s replaces all regex matches.

Sourceval replace_regex_flags : string -> string -> string -> string -> string

replace_regex_flags pattern flags replacement s replaces with specified flags.

Sourceval replace_all : string -> string -> string -> string

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

Sourceval replace_all_regex : string -> string -> string -> string

replace_all_regex pattern replacement s replaces all regex matches.

Sourceval split : string -> string -> string array

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

Sourceval split_limit : string -> int -> string -> string array

split_limit separator limit s splits with maximum limit parts.

Sourceval split_regex : string -> string -> string array

split_regex pattern s splits by regex pattern.