Crutches.String (imagine_cms v6.3.5)

Convenience functions for strings.

This module provides several convenience functions operating on strings. Simply call any function (with any options if applicable) to make use of it.

Link to this section Summary

Functions

Gives a substring of string from the given position.

Remove all occurrences of pattern from string.

Returns the string, first removing all whitespace on both ends of the string, and then changing remaining consecutive whitespace groups into one space each.

Capitalizes every word in a string. Similar to ActiveSupport's #titleize.

Returns a substring from the beginning of the string to the given position. If the position is negative, it is counted from the end of the string.

Truncates a given text after a given length if text is longer than length

Link to this section Functions

Link to this function

from(string, position)

Specs

from(String.t(), integer()) :: String.t()

Gives a substring of string from the given position.

If position is positive, counts from the start of the string. If position is negative, count from the end of the string.

Examples

iex> String.from("hello", 0)
"hello"

iex> String.from("hello", 3)
"lo"

iex> String.from("hello", -2)
"lo"

iex> String.from("hello", -7)
""

You can mix it with +to+ method and do fun things like:

iex> "hello"
iex> |> String.from(0)
iex> |> String.to(-1)
"hello"

iex> "hello"
iex> |> String.from(1)
iex> |> String.to(-2)
"ell"

iex> "a"
iex> |> String.from(1)
iex> |> String.to(1500)
""

iex> "elixir"
iex> |> String.from(-10)
iex> |> String.to(-7)
""
Link to this function

remove(string, patterns)

Specs

remove(String.t(), String.t() | Regex.t() | [any()]) :: String.t()

Remove all occurrences of pattern from string.

Can also take a list of patterns.

Examples

iex> String.remove("foo bar test", " test")
"foo bar"

iex> String.remove("foo bar test", ~r/foo /)
"bar test"

iex> String.remove("foo bar test", [~r/foo /, " test"])
"bar"

Specs

squish(String.t()) :: String.t()

Returns the string, first removing all whitespace on both ends of the string, and then changing remaining consecutive whitespace groups into one space each.

Examples

iex> str = "A multi line
iex> string"
iex> String.squish(str)
"A multi line string"

iex> str = " foo   bar    \n   \t   boo"
iex> String.squish(str)
"foo bar boo"
Link to this function

titlecase(string)

Specs

titlecase(String.t()) :: String.t()

Capitalizes every word in a string. Similar to ActiveSupport's #titleize.

iex> String.titlecase("the truth is rarely pure and never simple.") "The Truth Is Rarely Pure And Never Simple." iex> String.titlecase("THE TRUTH IS RARELY PURE AND NEVER SIMPLE.") "The Truth Is Rarely Pure And Never Simple." iex> String.titlecase("the truth is rarely pure and NEVER simple.") "The Truth Is Rarely Pure And Never Simple."

Link to this function

to(string, length)

Specs

to(String.t(), integer()) :: String.t()

Returns a substring from the beginning of the string to the given position. If the position is negative, it is counted from the end of the string.

Examples

iex> String.to("hello", 0)
"h"

iex> String.to("hello", 3)
"hell"

iex> String.to("hello", -2)
"hell"

You can mix it with +from+ method and do fun things like:

iex> "hello"
iex> |> String.from(0)
iex> |> String.to(-1)
"hello"

iex> "hello"
iex> |> String.from(1)
iex> |> String.to(-2)
"ell"
Link to this function

truncate(string, len, opts \\ [])

Truncates a given text after a given length if text is longer than length:

Truncates a given text after a given length if text is longer than lenth. The last characters will be replaced with the :omission (defaults to “...”) for a total length not exceeding len.

Pass a :separator to truncate text at a natural break (the first occurence of that separator before the provided length).

Examples

iex> String.truncate("Once upon a time in a world far far away", 27)
"Once upon a time in a wo..."

iex> String.truncate("Once upon a time in a world far far away", 27, separator: " ")
"Once upon a time in a..."

iex> String.truncate("Once upon a time in a world far far away", 27, separator: ~r/\s/)
"Once upon a time in a..."

iex> String.truncate("Once upon a time in a world far far away", 35, separator: "far ")
"Once upon a time in a world far..."

iex> String.truncate("And they found that many people were sleeping better.", 25, omission: "... (continued)")
"And they f... (continued)"

iex> String.truncate("Supercalifragilisticexpialidocious", 24, separator: ~r/\s/)
"Supercalifragilistice..."