Skip to content

String

capitalize(string)

Capitalizes the first word in the string

Examples:

$> capitalize("this is a string")
"This is a string"

downcase(string)

Converts a string to lowercase

Examples:

$> downcase("HOW ARE YOU?")
"how are you?"

ends_with?(string, suffix)

Returns true if string ends with any of the suffixes given.

suffixes can either be a single suffix or a list of suffixes.

$> ends_with?("language", "age")
true
$> ends_with?("language", ["youth", "age"])
true
$> ends_with?("language", ["youth", "bubble"])
false

An empty suffix will always match:

$> ends_with?("language", "")
true
$> ends_with?("language", ["", "other"])
true

An empty list will never match:

$> ends_with?("bubble", [])
false
$> ends_with?("", [])
false

html_entity_decode(string)

Decode HTML entities in a string.

html_entity_encode(string)

Encode HTML entities in a string.

pluralize(string)

Given a singular word, returns the plural version of it (according to English grammar)

Examples:

$> pluralize("Dog")
"Dogs"

regex_captures(string, regex, opts \\ [])

Run the regex until the first match.

Options:

  • :flags: A string containing the regex flags. Defaults to "u".

  • :capture: Can be one of the following:

    • :all - all captured subpatterns including the completing matching string (this is the default).

    • :first - only the first captured subpattern, which is always the complete matching part of the string; all explicitly captured subpatterns are discarded.

    • :all_but_first - all but the first matching subpattern, i.e. all explicitly captured subpatterns, but not the complete matching part of the string.

    • :none - does not return matching subpatterns at all.

    • :all_names - captures all named subpattern matches in the Regex as a list ordered alphabetically by the names of the subpatterns.

    • list(binary | atom) - a list of named captures to capture.

Examples:

# Matches
$> regex_captures("hello world", "(hello) world")
["hello world", "hello"]
$> regex_captures("hello world", "(hello) world", capture: :all_but_first)
["hello"]
$> regex_captures("ola world", "(?<greeting>hello|hi|ola) world", capture: ["greeting"])
["ola"]

# Only matches once.
$> regex_captures("hello ola hi hello", "(hello|hi|ola)", capture: :all_but_first)
["hello"]

# No match
$> regex_captures("", "(hello) world")
nil

regex_named_captures(string, regex, opts \\ [])

Run the regex until the first match, returning the named capture groups as a map.

This is the same as regex_captures/3 with the :capture option set to all named groups, and returning a map.

Options:

  • :flags: A string containing the regex flags. Defaults to "u".

Examples:

# Matches
$> regex_named_captures("ola name", "(?<greeting>hello|hi|ola).*(?<subject>world|name)")
%{"greeting" => "ola", "subject" => "name"}
$> regex_named_captures("hello world", "(?<greeting>hello|hi|ola).*(?<subject>world|name)")
%{"greeting" => "hello", "subject" => "world"}

# Only matches once.
$> regex_named_captures("hello ola hi hello", "(?<greeting>hello|hi|ola)")
%{"greeting" => "hello"}

# No match
$> regex_named_captures("", "(?<greeting>hello|hi|ola)")
nil

regex_replace(string, regex, replacement, options \\ [])

Replace matches of a regular expression in the given string with a replacement

Options:

  • flags: String with regex flags, defaults to "u" (unicode support). For case insensitive regexes, use "iu"

  • global: Boolean to check if regex needs to replace all occurrences or only the first one. Defaults to true.

Examples:

$> regex_replace("hello to all the cats", "cat", "dog")
"hello to all the dogs"
$> regex_replace("The cheese is 5 euros, eggs are 1 euro", "euros?", "pound")
"The cheese is 5 pound, eggs are 1 pound"

replace(string, pattern, replacement, options \\ [])

Replace string in the given string with a replacement. The pattern can be a string or a list of strings.

Examples:

$> replace("abc", "a", "b")
"bbc"
$> replace("abc", ["a", "b"], "")
"c"

singularize(string)

Given a plural word, returns the singular version of it (according to English grammar)

Examples:

$> singularize("dogs")
"dog"

starts_with?(string, prefix)

Returns true if string starts with any of the prefixes given.

prefix can either be a string or a list of strings.

Examples:

$> starts_with?("bubble", "bub")
true
$> starts_with?("bubble", ["flow", "bubble"])
true
$> starts_with?("bubble", ["thing", "other"])
false

An empty string will always match:

$> starts_with?("bubble", "")
true
$> starts_with?("bubble", ["", "other"])
true

An empty list will never match:

$> starts_with?("bubble", [])
false
$> starts_with?("", [])
false

string(value)

Coerce the given value into a string value

trim(string)

Trim whitespace characters from both ends of a string.

Examples:

$> trim("         slim me down        ")
"slim me down"

trim(string, what)

Trim given characters from both ends of a string.

Examples:

$> trim("^^^Remove^^^", "^")
"Remove"

trim_leading(string)

Trim leading whitespace characters from a string.

Examples:

$> trim_leading("      Leading")
"Leading"

trim_leading(string, what)

Trim given leading characters from a string

Examples:

$> trim_leading("***Stars", "*")
"Stars"

trim_trailing(string)

Trim trailing whitespace characters from a string

Examples:

$> trim_trailing("trailing   ")
"trailing"

trim_trailing(string, what)

Trim given trailing characters from a string

Examples:

$> trim_trailing("trailing))))", ")")
"trailing"

truncate(value, length, append \\ "…")

Truncates a string down to the number of characters passed as the first parameter.

An appendinx, defailting to the ellipsis character(…), is appended to the truncated string. The final string length is at max length characters, including the appendix.

Examples:

$> truncate("a very long string", 2)
"a…"

truncate_words(value, numwords, append \\ "…")

Truncates a string down to the number of words passed as the first parameter. An ellipsis (…) is appended to the truncated string.

Examples:

$> truncate_words("Truncates a string down to the number of words passed", 3)
"Truncates a string…"
$> truncate_words("Truncates a string down to the number of words passed", 3, ", read more")
"Truncates a string, read more"

upcase(string)

Converts a string to uppercase

Examples:

$> upcase("am I shouting?")
"AM I SHOUTING?"

url_decode(string)

Decode escaped URL characters

Examples:

$> url_decode("a%3Dvalue")
"a=value"

url_encode(var)

Converts any URL-unsafe characters in a string into percent-encoded characters. Note that url_encode will turn a space into a + sign instead of a percent-encoded character.

Examples:

$> url_encode("var=this is a value")
"var%3Dthis+is+a+value"

url_escape(var)

Escapes any URL-unsafe characters in a string into percent-encoded characters, but leaves reserved characters alone (/, &, :, ?)

url_params(var)

Convert the given list of key/value pairs into a string which can be used in a URL query string.

Examples:

$> url_params([page: 1, id: 2])
"page=1&id=2"

url_parse(string)

Parses a URL.

Examples:

$> url_parse("https://example.com/path/to/file?id=5#download") %{ scheme: "https", userinfo: nil, host: "example.com", port: 443, path: "/path/to/file", query: "id=5", query_params: %{"id" => "5"}, fragment: "download", authority: nil }