Crutches.String (imagine_cms v6.3.7)
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
from(string, position)
Specs
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)
""
remove(string, patterns)
Specs
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"
squish(string)
Specs
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"
titlecase(string)
Specs
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."
to(string, length)
Specs
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"
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 len
gth if text is longer than len
th.
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..."