StringxSourcecenter s len pad centers s in a string of length len, padding with pad. If s is longer than len, it is returned unchanged. Padding is inserted symmetrically. pad must be non-empty or it is ignored.
This function is Unicode-aware and counts characters, not bytes. If pad is multibyte, it is repeated and truncated as needed.
Examples:
center "hello" 10 " " returns " hello "center "abc" 7 "ใ" returns "ใใabcใใ"count str pattern counts how many Unicode characters in str match pattern.
The pattern supports:
This function is Unicode-aware and handles UTF-8 properly.
Examples:
count "hello" "aeiou" returns 2count "abc123" "^a-z" returns 3count "ใใใซใกใฏ" "ใ-ใ" returns 5delete str pattern removes all Unicode characters in str that match pattern.
The pattern supports:
This function is Unicode-aware and handles UTF-8 properly.
Examples:
delete "hello" "aeiou" returns "hll"delete "ใใใซใกใฏ" "ใ" returns "ใใซใกใฏ"delete "abc123" "^a-z" returns "abc"len str returns the number of Unicode code points (runes) in UTF-8 string str.
This function is Unicode-aware and counts characters, not bytes.
Examples:
len "hello" returns 5len "ใใใซใกใฏ" returns 5len "๐๐๐" returns 3reverse s reverses a UTF-8 encoded string s.
This function is Unicode-aware and reverses by code points, not bytes.
Examples:
reverse "hello" returns "olleh"reverse "ใใใซใกใฏ" returns "ใฏใกใซใใ"reverse "๐๐๐" returns "๐๐๐"contains s substr reports whether substr is within s.
Returns true if substr is the empty string, or if substr occurs anywhere in s. Returns false otherwise.
This function is Unicode-agnostic and operates on bytes, not code points.
Examples:
contains "seafood" "foo" returns truecontains "seafood" "bar" returns falsecontains "seafood" "" returns truecontains "" "" returns truecontains_any s chars reports whether any Unicode code points in chars are within s.
Returns false if chars is empty. Unicode-aware and compares by code points.
Examples:
contains_any "team" "i" returns falsecontains_any "fail" "ui" returns truecontains_any "ure" "ui" returns truecontains_any "failure" "ui" returns truecontains_any "foo" "" returns falsecontains_any "" "" returns falsehas_prefix s prefix reports whether the string s begins with prefix.
Returns true if prefix is the empty string, or if s starts with prefix. Returns false otherwise.
This function is Unicode-agnostic and operates on bytes, not code points.
Examples:
has_prefix "Gopher" "Go" returns truehas_prefix "Gopher" "C" returns falsehas_prefix "Gopher" "" returns truehas_suffix s suffix reports whether the string s ends with suffix.
Returns true if suffix is the empty string, or if s ends with suffix. Returns false otherwise.
This function is Unicode-agnostic and operates on bytes, not code points.
Examples:
has_suffix "Amigo" "go" returns truehas_suffix "Amigo" "O" returns falsehas_suffix "Amigo" "Ami" returns falsehas_suffix "Amigo" "" returns truecount_substring s substr counts the number of non-overlapping instances of substr in s.
If substr is the empty string, returns 1 + the number of Unicode code points in s.
This function is Unicode-agnostic and operates on bytes, not code points.
Examples:
count_substring "cheese" "e" returns 3count_substring "five" "" returns 5count_substring "banana" "na" returns 2count_substring "aaaaa" "aa" returns 2count_substring "" "" returns 1count_substring "" "a" returns 0equal_fold s t reports whether s and t, interpreted as UTF-8 strings, are equal under simple Unicode case-folding (ASCII only).
This is a simple case-insensitive comparison for ASCII letters only. (It does not perform full Unicode case folding.)
Examples:
equal_fold "Go" "go" returns trueequal_fold "AB" "ab" returns trueequal_fold "ร" "ss" returns falsefields s splits the string s around each instance of one or more consecutive Unicode whitespace characters, returning a list of substrings of s or an empty list if s contains only whitespace.
Whitespace is defined by Unicode (see is_space).
Examples:
fields " foo bar baz " returns ["foo"; "bar"; "baz"]fields " " returns []fields "a\tb\nc" returns ["a"; "b"; "c"]fields_func s f splits the string s at each run of Unicode code points c satisfying f c, returning a list of substrings of s or an empty list if all code points in s satisfy f or s is empty.
Examples:
fields_func " foo1;bar2,baz3..." (fun c -> not (is_letter c || is_number c)) returns ["foo1"; "bar2"; "baz3"]index s substr returns the index of the first instance of substr in s, or -1 if substr is not present.
The index is a byte offset (not code point index).
Examples:
index "chicken" "ken" returns 4index "chicken" "dmr" returns -1index "abc" "" returns 0index "" "" returns 0index "" "a" returns -1repeat s count returns a new string consisting of count copies of s.
Raises Invalid_argument if count is negative.
Examples:
repeat "na" 2 returns "nana"repeat "๐" 3 returns "๐๐๐"repeat "" 5 returns ""repeat "a" 0 returns ""repeat "abc" (-1) raises Invalid_argumentjoin elems sep concatenates the elements of elems, inserting sep between each element.
Returns the empty string if elems is empty.
Examples:
join ["foo"; "bar"; "baz"] ", " returns "foo, bar, baz"join [] ", " returns ""join ["a"] ", " returns "a"