Crutches.String

Summary

camelize(underscore)

Converts strings to UpperCamelCase

from(string, start)

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

remove(string, to_remove)

Returns a new string with all occurrences of the patterns removed

squish(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

to(string, length)

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

underscore(camel_case)

Makes an underscored, lowercase form from the expression in the string. +underscore+ will also change ‘.’ to ‘/‘ to convert namespaces to paths

Types

t :: String

Functions

camelize(underscore)

Specs:

  • camelize(t) :: t

Converts strings to UpperCamelCase.

Examples

iex> String.camelize("product")
"Product"

iex> String.camelize("special_guest")
"SpecialGuest"

iex> String.camelize("application_controller")
"ApplicationController"

iex> String.camelize("area51_controller")
"Area51Controller"
from(string, start)

Specs:

Returns a substring from the given position to the end of the string. If the position is negative, it is counted 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, to_remove)

Specs:

Returns a new string with all occurrences of the patterns removed.

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:

  • squish(t) :: 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"
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"
underscore(camel_case)

Specs:

  • underscore(t) :: t

Makes an underscored, lowercase form from the expression in the string. +underscore+ will also change ‘.’ to ‘/‘ to convert namespaces to paths.

Examples

iex> String.underscore("Product")
"product"

iex> String.underscore("SpecialGuest")
"special_guest"

iex> String.underscore("ApplicationController")
"application_controller"

iex> String.underscore("Area51Controller")
"area51_controller"

["HTMLTidyGenerator",  "html_tidy_generator"],

["UsersSection.Commission.Department", "users_section/commission/department"],