String

Core

A built-in representation for efficient string manipulation. String literals are enclosed in "double quotes". Strings are *not* lists of characters. On the BEAM target, length and indexing are UTF-8 character based.

Values

isEmpty

isEmpty : String -> Bool

Determine if a string is empty.

Examples

isEmpty "" == True
isEmpty "the world" == False

length

length : String -> Int

Get the length of a string.

Examples

length "innumerable" == 11
length "" == 0

reverse

reverse : String -> String

Reverse a string.

Examples

reverse "stressed" == "desserts"

repeat

repeat : Int -> String -> String

Repeat a string *n* times.

Examples

repeat 3 "ha" == "hahaha"

replace

replace : String -> String -> String -> String

Replace all occurrences of some substring.

Examples

replace "." "-" "Json.Decode.succeed" == "Json-Decode-succeed"
replace "," "/" "a,b,c,d,e"           == "a/b/c/d/e"

append

append : String -> String -> String

Append two strings. You can also use the (++) operator to do this.

Examples

append "butter" "fly" == "butterfly"

concat

concat : List String -> String

Concatenate many strings into one.

Examples

concat ["never","the","less"] == "nevertheless"

split

split : String -> String -> List String

Split a string using a given separator.

Examples

split "," "cat,dog,cow"        == ["cat","dog","cow"]
split "/" "home/evan/Desktop/" == ["home","evan","Desktop", ""]

join

join : String -> List String -> String

Put many strings together with a given separator.

Examples

join "a" ["H","w","ii","n"]        == "Hawaiian"
join " " ["cat","dog","cow"]       == "cat dog cow"
join "/" ["home","evan","Desktop"] == "home/evan/Desktop"

words

words : String -> List String

Break a string into words, splitting on chunks of whitespace.

Examples

words "How are \t you? \n Good?" == ["How","are","you?","Good?"]

lines

lines : String -> List String

Break a string into lines, splitting on newlines.

Examples

lines "How are you?\nGood?" == ["How are you?", "Good?"]

slice

slice : Int -> Int -> String -> String

Take a substring given a start and end index. Negative indexes are taken starting from the *end* of the string.

Examples

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"

left

left : Int -> String -> String

Take *n* characters from the left side of a string.

Examples

left 2 "Mulder" == "Mu"

dropLeft

dropLeft : Int -> String -> String

Drop *n* characters from the left side of a string.

Examples

dropLeft 2 "The Lone Gunmen" == "e Lone Gunmen"

dropRight

dropRight : Int -> String -> String

Drop *n* characters from the right side of a string.

Examples

dropRight 2 "Cigarette Smoking Man" == "Cigarette Smoking M"

contains

contains : String -> String -> Bool

See if the second string contains the first one.

Examples

contains "the" "theory" == True
contains "hat" "theory" == False
contains "THE" "theory" == False

startsWith

startsWith : String -> String -> Bool

See if the second string starts with the first one.

Examples

startsWith "the" "theory" == True
startsWith "ory" "theory" == False

endsWith

endsWith : String -> String -> Bool

See if the second string ends with the first one.

Examples

endsWith "the" "theory" == False
endsWith "ory" "theory" == True

indexes

indexes : String -> String -> List Int

Get all of the indexes for a substring in another string.

Examples

indexes "i" "Mississippi"   == [1,4,7,10]
indexes "ss" "Mississippi"  == [2,5]
indexes "needle" "haystack" == []

indices

indices : String -> String -> List Int

Alias for indexes.

Examples

indices "na" "banana" == [2,4]

toInt

toInt : String -> Maybe Int

Try to convert a string into an int, failing on improperly formatted strings.

Examples

String.toInt "123" == Just 123
String.toInt "-42" == Just -42
String.toInt "3.1" == Nothing
String.toInt "31a" == Nothing

If you are extracting a number from some raw user input, you will typically want to use Maybe.withDefault to handle bad data:

Maybe.withDefault 0 (String.toInt "42") == 42
Maybe.withDefault 0 (String.toInt "ab") == 0

toFloat

toFloat : String -> Maybe Float

Try to convert a string into a float, failing on improperly formatted strings.

Examples

String.toFloat "123" == Just 123.0
String.toFloat "-42" == Just -42.0
String.toFloat "3.1" == Just 3.1
String.toFloat "31a" == Nothing

If you are extracting a number from some raw user input, you will typically want to use Maybe.withDefault to handle bad data:

Maybe.withDefault 0 (String.toFloat "42.5") == 42.5
Maybe.withDefault 0 (String.toFloat "cats") == 0

fromInt

fromInt : Int -> String

Convert an Int to a String.

Examples

String.fromInt 123 == "123"
String.fromInt -42 == "-42"

Check out Debug.toString to convert *any* value to a string for debugging.

fromFloat

fromFloat : Float -> String

Convert a Float to a String.

Examples

String.fromFloat 123 == "123"
String.fromFloat -42 == "-42"
String.fromFloat 3.9 == "3.9"

Check out Debug.toString to convert *any* value to a string for debugging.

toList

toList : String -> List Char

Convert a string to a list of characters.

Examples

toList "abc" == ['a','b','c']
toList "🙈🙉🙊" == ['🙈','🙉','🙊']

fromList

fromList : List Char -> String

Convert a list of characters into a String. Can be useful if you want to create a string primarily by consing, perhaps for decoding something.

Examples

fromList ['a','b','c'] == "abc"
fromList ['🙈','🙉','🙊'] == "🙈🙉🙊"

fromChar

fromChar : Char -> String

Create a string from a given character.

Examples

fromChar 'a' == "a"

cons

cons : Char -> String -> String

Add a character to the beginning of a string.

Examples

cons 'T' "he truth is out there" == "The truth is out there"

toUpper

toUpper : String -> String

Convert a string to all upper case. Useful for case-insensitive comparisons and VIRTUAL YELLING.

Examples

toUpper "skinner" == "SKINNER"

toLower

toLower : String -> String

Convert a string to all lower case. Useful for case-insensitive comparisons.

Examples

toLower "X-FILES" == "x-files"

trim

trim : String -> String

Get rid of whitespace on both sides of a string.

Examples

trim "  hats  \n" == "hats"

trimLeft

trimLeft : String -> String

Get rid of whitespace on the left of a string.

Examples

trimLeft "  hats  \n" == "hats  \n"

trimRight

trimRight : String -> String

Get rid of whitespace on the right of a string.

Examples

trimRight "  hats  \n" == "  hats"

pad

pad : Int -> Char -> String -> String

Pad a string on both sides until it has a given length.

Examples

pad 5 ' ' "1"   == "  1  "
pad 5 ' ' "11"  == "  11 "
pad 5 ' ' "121" == " 121 "

padLeft

padLeft : Int -> Char -> String -> String

Pad a string on the left until it has a given length.

Examples

padLeft 5 '.' "1"   == "....1"
padLeft 5 '.' "11"  == "...11"
padLeft 5 '.' "121" == "..121"

padRight

padRight : Int -> Char -> String -> String

Pad a string on the right until it has a given length.

Examples

padRight 5 '.' "1"   == "1...."
padRight 5 '.' "11"  == "11..."
padRight 5 '.' "121" == "121.."