Skip to content

String

capitalize(string)

Capitalizes the first word in the string

Examples:

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

downcase(string)

Converts a string to lowercase

Examples:

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

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:

iex> pluralize("Dog")
"Dogs"

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:

iex> regex_replace("hello to all the cats", "cat", "dog")
"hello to all the dogs"
iex> 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:

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

singularize(string)

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

Examples:

iex> singularize("dogs")
"dog"

string(value)

Coerce the given value into a string value

trim(string)

Trim whitespace characters from both ends of a string.

Examples:

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

trim(string, what)

Trim given characters from both ends of a string.

Examples:

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

trim_leading(string)

Trim leading whitespace characters from a string.

Examples:

iex> trim_leading("      Leading")
"Leading"

trim_leading(string, what)

Trim given leading characters from a string

Examples:

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

trim_trailing(string)

Trim trailing whitespace characters from a string

Examples:

iex> trim_trailing("trailing   ")
"trailing"

trim_trailing(string, what)

Trim given trailing characters from a string

Examples:

iex> 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:

iex> 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:

iex> truncate_words("Truncates a string down to the number of words passed", 3)
"Truncates a string…"
iex> 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:

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

url_decode(string)

Decode escaped URL characters

Examples:

iex> 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:

iex> 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:

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

url_parse(string)

Parses a URL.

Examples:

iex> 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 }