Additional functions for working with Strings
Capitalize the first character of each word in a string.
toTitleCase "this is a phrase" == "This Is A Phrase"
toTitleCase "hello, world" == "Hello, World"Decapitalize the first letter of a string.
decapitalize "This is a phrase" == "this is a phrase"
decapitalize "Hello, World" == "hello, World"Functions borrowed from the Rails Inflector class
Convert an underscored or dasherized string to a camelized one.
camelize "-moz-transform" == "MozTransform"Convert a string to a camelized string starting with an uppercase letter. All non-word characters will be stripped out of the original string.
classify "some_class_name" == "SomeClassName"
classify "myLittleCamel.class.name" == "MyLittleCamelClassName"Return a string joined by underscores after separating it by its uppercase characters. Any sequence of spaces or dashes will also be converted to a single underscore. The final string will be lowercased.
underscore "SomeClassName" == "some_class_name"
underscore "some-class-name" == "some_class_name"
underscore "SomeClass name" == "some_class_nameReturn a string joined by dashes after separating it by its uppercase characters. Any sequence of spaces or underscores will also be converted to a single dash. The final string will be lowercased.
dasherize "SomeClassName" == "-some-class-name"
dasherize "some_class_name" = "some-class-name"
dasherize "someClass name" = "some-class-name"Convert an underscored, camelized, or dasherized string into one that can be read by humans. Also remove beginning and ending whitespace, and removes the postfix '_id'. The first character will be capitalized.
humanize "this_is_great" == "This is great"
humanize "ThisIsGreat" = "This is great"
humanize "this-is-great" = "This is great"
humanize "author_id" = "Author"Replace all occurrences of the search string with the substitution string.
replace "Mary" "Sue" "Hello, Mary" == "Hello, Sue"Replace text within a portion of a string given a substitution string, a start index and an end index. The substitution includes the character at the start index but not the one at the end index.
replaceSlice "Sue" 4 7 "Hi, Bob" == "Hi, Sue"
replaceSlice "elephants" 0 6 "snakes on a plane!" == "elephants on a plane!"
replaceSlice "under" 7 9 "snakes on a plane!" == "snakes under a plane!"Insert a substring at the specified index.
insertAt "world" 6 "Hello " == "Hello world"Convert a string to a Nothing when empty.
nonEmpty "" == Nothing
nonEmpty "Hello world" == Just "Hello world"Convert a string to a Nothing when blank.
nonBlank "" == Nothing
nonBlank "Hello world" == Just "Hello world"Remove accents from string.
removeAccents "andré" == "andre"
removeAccents "Atenção" == "Atencao"Break a string into a list of strings of a specified maximum length.
break 10 "The quick brown fox" == ["The quick ", "brown fox"]
break 2 "" == [""]Break a string into a list of strings of a specified maximum length, without truncating words.
softBreak 6 "The quick brown fox" == ["The quick", " brown", " fox"]Chop a given string into parts of a given width, separating them with a new line.
wrap 7 "My very long text" === "My very\nlong te\nxt"
wrap 100 "Too short" === "Too short"Separate a string into parts of a given width, using a given separator.
Look at wrap if you just want to wrap using newlines.
wrapWith 7 "\n" "My very long text" === "My very\nlong text"
wrapWith 100 "\n" "Too short" === "Too short"Chop a given string into parts of a given width without breaking words apart, and then separate them using a new line.
softWrap 7 "My very long text" === "My very\nlong text"
softWrap 3 "Hello World" === "Hello \nWorld"
softWrap 100 "Too short" === "Too short"Chop a given string into parts of a given width without breaking words apart, and then separate them using the given separator.
softWrapWith 7 "..." "My very long text" === "My very...long text"
softWrapWith 3 "\n" "Hello World" === "Hello \nWorld"
softWrapWith 100 "\t" "Too short" === "Too short"Add quotes to a string.
quote "foo" == "\"foo\""Surround a string with another string.
surround "bar" "foo" == "barfoobar"Test if a string is empty or only contains whitespace.
isBlank "" == True
isBlank "\n" == True
isBlank " " == True
isBlank " a" == FalseReturn the number of occurrences of a substring in another string.
countOccurrences "Hello" "Hello World" == 1
countOccurrences "o" "Hello World" == 2Trim the whitespace of both sides of the string and compress repeated whitespace internally to a single whitespace char.
clean " The quick brown fox " == "The quick brown fox"Remove quotes that surround a string.
unquote "\"foo\"" == "foo"
unquote "\"foo\"bar\""Remove surrounding strings from another string.
unsurround "foo" "foobarfoo" == "bar"Remove the shortest sequence of leading spaces or tabs on each line of the string, so that at least one of the lines will not have any leading spaces nor tabs and the rest of the lines will have the same amount of indentation removed.
unindent " Hello\n World " == "Hello\n World"
unindent "\t\tHello\n\t\t\t\tWorld" == "Hello\n\t\tWorld"Truncate the string at the specified length if the string is
longer than the specified length, and replace the end of the truncated
string with "...", such that the resulting string is of the
specified length.
The resulting string will have at most the specified length.
ellipsis 5 "Hello World" == "He..."
ellipsis 10 "Hello World" == "Hello W..."
ellipsis 10 "Hello" == "Hello"
ellipsis 8 "Hello World" == "Hello..."Truncate the string at the last complete word less than or equal to
the specified length and append "...". When the specified length is
less than the length of the first word, the ellipsis is appended to the
first word. When the specified length is greater than or equal to the
length of the string, an identical string is returned.
In contrast to ellipsis, this function will not produce incomplete
words, and the resulting string can exceed the specified length. In
addition, it removes trailing whitespace and punctuation characters at
the end of the truncated string.
softEllipsis 1 "Hello, World" == "Hello..."
softEllipsis 5 "Hello, World" == "Hello..."
softEllipsis 6 "Hello, World" == "Hello..."
softEllipsis 15 "Hello, cruel world" == "Hello, cruel..."
softEllipsis 10 "Hello" == "Hello"Truncate the second string at the specified length if the string is longer than the specified length, and replace the end of the truncated string with the first string, such that the resulting string is of the specified length.
The resulting string will have at most the specified length.
ellipsisWith 5 " .." "Hello World" == "He .."
ellipsisWith 10 " .." "Hello World" == "Hello W .."
ellipsisWith 10 " .." "Hello" == "Hello"
ellipsisWith 8 " .." "Hello World" == "Hello .."Remove all HTML tags from the string, preserving the text inside them.
stripTags "a <a href=\"#\">link</a>" == "a link"
stripTags "<script>alert('hello world!')</script> == "alert('hello world!')"Given a number, a singular string, and a plural string, return the number followed by a space, followed by either the singular string if the number was 1, or the plural string otherwise.
pluralize "elf" "elves" 2 == "2 elves"
pluralize "elf" "elves" 1 == "1 elf"
pluralize "elf" "elves" 0 == "0 elves"Convert a list of strings into a human-readable list.
toSentence [] == ""
toSentence ["lions"] == "lions"
toSentence ["lions", "tigers"] == "lions and tigers"
toSentence ["lions", "tigers", "bears"] == "lions, tigers and bears"Convert a list of strings into a human-readable list using an oxford comma.
toSentenceOxford [] == ""
toSentenceOxford ["lions"] == "lions"
toSentenceOxford ["lions", "tigers"] == "lions and tigers"
toSentenceOxford ["lions", "tigers", "bears"] == "lions, tigers, and bears"Search a string from left to right for a pattern and return a substring consisting of the characters in the string that are to the right of the pattern.
rightOf "_" "This_is_a_test_string" == "is_a_test_string"Search a string from left to right for a pattern and return a substring consisting of the characters in the string that are to the left of the pattern.
leftOf "_" "This_is_a_test_string" == "This"Search a string from right to left for a pattern and return a substring consisting of the characters in the string that are to the right of the pattern.
rightOfBack "_" "This_is_a_test_string" == "string"Search a string from right to left for a pattern and return a substring consisting of the characters in the string that are to the left of the pattern.
leftOfBack "_" "This_is_a_test_string" == "This_is_a_test"Convert a string into a list of UTF-32 code points.
toCodePoints "abc" == [ 97, 98, 99 ]
toCodePoints "©§π" == [ 169, 167, 960 ]
toCodePoints "💩!" == [ 128169, 33 ]Note that code points do not necessarily correspond to logical/visual characters, since it is possible for things like accented characters to be represented as two separate UTF-32 code points (a base character and a combining accent).
toCodePoints string is equivalent to:
List.map Char.toCode (String.toList string)Convert a list of UTF-32 code points into a string. Inverse of
toCodePoints.
fromCodePoints [ 97, 98, 99 ] == "abc"
fromCodePoints [ 169, 167, 960 ] == "©§π"
fromCodePoints [ 128169, 33 ] == "💩!"fromCodePoints codePoints is equivalent to:
String.fromList (List.map Char.fromCode codePoints)
Capitalize the first letter of a string.