A built-in representation for efficient string manipulation. String literals
are enclosed in "double quotes". Strings are not lists of characters.
Get the length of a string.
length "innumerable" == 11
length "" == 0Reverse a string.
reverse "stressed" == "desserts"Repeat a string n times.
repeat 3 "ha" == "hahaha"Add a character to the beginning of a string.
Split a non-empty string into its head and tail. This lets you pattern match on strings exactly as you would with lists.
uncons "abc" == Just ('a',"bc")
uncons "" == NothingCreate a string from a given character.
fromChar 'a' == "a"Append two strings. You can also use the (++) operator
to do this.
append "butter" "fly" == "butterfly"Concatenate many strings into one.
concat ["never","the","less"] == "nevertheless"Split a string using a given separator.
split "," "cat,dog,cow" == ["cat","dog","cow"]
split "/" "home/evan/Desktop/" == ["home","evan","Desktop", ""]Use Regex.split if you need something more flexible.
Put many strings together with a given separator.
join "a" ["H","w","ii","n"] == "Hawaiian"
join " " ["cat","dog","cow"] == "cat dog cow"
join "/" ["home","evan","Desktop"] == "home/evan/Desktop"Break a string into words, splitting on chunks of whitespace.
words "How are \t you? \n Good?" == ["How","are","you?","Good?"]Break a string into lines, splitting on newlines.
lines "How are you?\nGood?" == ["How are you?", "Good?"]Take a substring given a start and end index. Negative indexes are taken starting from the end of the list.
slice 7 9 "snakes on a plane!" == "on"
slice 0 6 "snakes on a plane!" == "snakes"
slice 0 -7 "snakes on a plane!" == "snakes on a"
slice -6 -1 "snakes on a plane!" == "plane"Take n characters from the left side of a string.
Take n characters from the right side of a string.
Drop n characters from the left side of a string.
Drop n characters from the right side of a string.
See if the second string contains the first one.
contains "the" "theory" == True
contains "hat" "theory" == False
contains "THE" "theory" == FalseUse Regex.contains if you need something more flexible.
See if the second string starts with the first one.
startsWith "the" "theory" == True
startsWith "ory" "theory" == FalseSee if the second string ends with the first one.
endsWith "the" "theory" == False
endsWith "ory" "theory" == TrueGet all of the indexes for a substring in another string.
indexes "i" "Mississippi" == [1,4,7,10]
indexes "ss" "Mississippi" == [2,5]
indexes "needle" "haystack" == []Alias for indexes
Try to convert a string into an int, failing on improperly formatted strings.
toInt "123" == Ok 123
toInt "-42" == Ok -42
toInt "3.1" == Err "could not convert string '3.1' to an Int"
toInt "31a" == Err "could not convert string '31a' to an Int"Try to convert a string into a float, failing on improperly formatted strings.
toFloat "123" == Ok 123.0
toFloat "-42" == Ok -42.0
toFloat "3.1" == Ok 3.1
toFloat "31a" == Err "could not convert string '31a' to a Float"Convert a string to a list of characters.
toList "abc" == ['a','b','c']Convert a list of characters into a String. Can be useful if you want to create a string primarly by consing, perhaps for decoding something.
fromList ['a','b','c'] == "abc"Cosmetic operations such as padding with extra characters or trimming whitespace.
Convert a string to all upper case. Useful for case insensitive comparisons and VIRTUAL YELLING.
Convert a string to all lower case. Useful for case insensitive comparisons.
Pad a string on both sides until it has a given length.
pad 5 ' ' "1" == " 1 "
pad 5 ' ' "11" == " 11 "
pad 5 ' ' "121" == " 121 "Pad a string on the left until it has a given length.
padLeft 5 '.' "1" == "....1"
padLeft 5 '.' "11" == "...11"
padLeft 5 '.' "121" == "..121"Pad a string on the right until it has a given length.
padRight 5 '.' "1" == "1...."
padRight 5 '.' "11" == "11..."
padRight 5 '.' "121" == "121.."Get rid of whitespace on both sides of a string.
trim " hats \n" == "hats"Get rid of whitespace on the left of a string.
trimLeft " hats \n" == "hats \n"Get rid of whitespace on the right of a string.
trimRight " hats \n" == " hats"Transform every character in a string
map (\c -> if c == '/' then '.' else c) "a/b/c" == "a.b.c"Keep only the characters that satisfy the predicate.
filter isDigit "R2-D2" == "22"Reduce a string from the left.
foldl cons "" "time" == "emit"Reduce a string from the right.
foldr cons "" "time" == "time"Determine whether any characters satisfy a predicate.
any isDigit "90210" == True
any isDigit "R2-D2" == True
any isDigit "heart" == FalseDetermine whether all characters satisfy a predicate.
all isDigit "90210" == True
all isDigit "R2-D2" == False
all isDigit "heart" == False
Determine if a string is empty.