Additional functions for working with Strings
toSentenceCase : String -> String
Capitalize the first letter of a string.
toSentenceCase "this is a phrase" == "This is a phrase"
toSentenceCase "hello, world" == "Hello, world"
toTitleCase : String -> String
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 : String -> String
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
camelize : String -> String
Convert an underscored or dasherized string to a camelized one.
camelize "-moz-transform" == "MozTransform"
classify : String -> String
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"
underscored : String -> String
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.
underscored "SomeClassName" == "some_class_name"
underscored "some-class-name" == "some_class_name"
underscored "SomeClass name" == "some_class_name
dasherize : String -> String
Return 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"
humanize : String -> String
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"
replaceSlice : String -> Basics.Int -> Basics.Int -> String -> String
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!"
insertAt : String -> Basics.Int -> String -> String
Insert a substring at the specified index.
insertAt "world" 6 "Hello " == "Hello world"
nonEmpty : String -> Maybe String
Convert a string to a Nothing when empty.
nonEmpty "" == Nothing
nonEmpty "Hello world" == Just "Hello world"
nonBlank : String -> Maybe String
Convert a string to a Nothing when blank.
nonBlank "" == Nothing
nonBlank "Hello world" == Just "Hello world"
removeAccents : String -> String
Remove accents from string.
removeAccents "andré" == "andre"
removeAccents "Atenção" == "Atencao"
break : Basics.Int -> String -> List String
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 "" == [ "" ]
softBreak : Basics.Int -> String -> List String
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" ]
wrap : Basics.Int -> String -> String
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"
wrapWith : Basics.Int -> String -> String -> String
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"
softWrap : Basics.Int -> String -> String
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"
softWrapWith : Basics.Int -> String -> String -> String
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"
quote : String -> String
Add quotes to a string.
quote "foo" == "\"foo\""
surround : String -> String -> String
Surround a string with another string.
surround "bar" "foo" == "barfoobar"
isBlank : String -> Basics.Bool
Test if a string is empty or only contains whitespace.
isBlank "" == True
isBlank "\n" == True
isBlank " " == True
isBlank " a" == False
countOccurrences : String -> String -> Basics.Int
Return the number of occurrences of a substring in another string.
countOccurrences "Hello" "Hello World" == 1
countOccurrences "o" "Hello World" == 2
clean : String -> String
Trim 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"
unquote : String -> String
Remove quotes that surround a string.
unquote "\"foo\"" == "foo"
unquote "\"foo\"bar\""
unsurround : String -> String -> String
Remove surrounding strings from another string.
unsurround "foo" "foobarfoo" == "bar"
unindent : String -> String
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"
ellipsis : Basics.Int -> String -> String
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..."
softEllipsis : Basics.Int -> String -> String
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"
ellipsisWith : Basics.Int -> String -> String -> String
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 .."
stripTags : String -> String
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!')"
pluralize : String -> String -> Basics.Int -> String
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"
toSentence : List String -> String
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"
toSentenceOxford : List String -> String
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"
rightOf : String -> String -> 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 right of the pattern.
rightOf "_" "This_is_a_test_string" == "is_a_test_string"
leftOf : String -> String -> 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"
rightOfBack : String -> 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 right of the pattern.
rightOfBack "_" "This_is_a_test_string" == "string"
leftOfBack : String -> 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"
toCodePoints : String -> List Basics.Int
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)
fromCodePoints : List Basics.Int -> 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)