#string
Operations on String and Char.
A string is an immutable sequence of Unicode codepoints, and a Char is
one codepoint. Positions and lengths count codepoints, not bytes and not
grapheme clusters. Character classification and case mapping are ASCII
only: a non-ASCII character is never a letter, digit, or space to these
functions, and passes through toUpper and toLower unchanged.
length and isEmpty are not defined here, to leave the Foldable
methods of those names unshadowed. Use stringLength s and s == "".
intToString renders an integer.
#Characters
#isDigit
isDigit : Char -> Bool
isDigit cWhether c is an ASCII decimal digit, '0' to '9'.
> isDigit '7'
True
> isDigit 'x'
False#isAlpha
isAlpha : Char -> Bool
isAlpha cWhether c is an ASCII letter.
#isAlphaNum
isAlphaNum : Char -> Bool
isAlphaNum cWhether c is an ASCII letter or digit.
#isSpace
isSpace : Char -> Bool
isSpace cWhether c is ASCII whitespace.
#isUpper
isUpper : Char -> Bool
isUpper cWhether c is an ASCII uppercase letter.
#isLower
isLower : Char -> Bool
isLower cWhether c is an ASCII lowercase letter.
#isPunct
isPunct : Char -> Bool
isPunct cWhether c is ASCII punctuation.
#fromDigit
fromDigit : Char -> Option Int
fromDigit cThe value of a hexadecimal digit: '0' to '9' give 0 to 9, and
'a' to 'f' or 'A' to 'F' give 10 to 15. None for any other
character.
> fromDigit '7'
Some 7
> fromDigit 'f'
Some 15#toDigit
toDigit : Int -> Option Char
toDigit nThe lowercase hexadecimal digit for a value from 0 to 15, or None
outside that range. The inverse of fromDigit.
> toDigit 7
Some '7'
> toDigit 12
Some 'c'#Conversion
#fromChar
fromChar : Char -> String
fromChar cA string holding one character.
#toChars
toChars : String -> Array Char
toChars sThe codepoints of a string, as an array.
array.toList turns the result into a List Char when one is needed.
> arrayLength (toChars "héllo→")
6#fromChars
fromChars : List Char -> String
fromChars csA string built from a list of characters.
For an Array Char, such as the result of toChars, use
stringFromChars.
> fromChars ['h', 'i']
"hi"#toUtf8
toUtf8 : String -> Array Int
toUtf8 sThe UTF-8 encoding of a string, one byte (0 to 255) per element.
A codepoint outside ASCII contributes several bytes; toChars gives the
codepoints instead.
> arrayLength (toUtf8 "héllo")
6#fromUtf8
fromUtf8 : Array Int -> String
fromUtf8 bytesThe string encoded by an array of UTF-8 bytes.
Only the low eight bits of each element are used. Each ill-formed sequence
(a stray continuation byte, a truncated sequence, an overlong form, a
surrogate, anything above U+10FFFF) becomes one U+FFFD replacement
character per maximal subpart, as bytes.decodeUtf8Lossy does, so
fromUtf8 (toUtf8 s) is s for every string, but toUtf8 (fromUtf8 bytes)
is bytes only when bytes is valid UTF-8. Keep bytes that are not text in
a bytes.Bytes.
> fromUtf8 (toUtf8 "héllo→")
"héllo→"#utf8ByteLength
utf8ByteLength : String -> Int
utf8ByteLength sThe number of bytes in the string's UTF-8 encoding.
At least the codepoint count, and larger when the string has non-ASCII characters.
> utf8ByteLength "héllo"
6#toInt
toInt : String -> Option Int
toInt sThe integer written in decimal in s, with an optional leading - or
+.
None when s is empty, contains any other character, or names a value
outside the Int range.
> toInt "42"
Some 42
> toInt "12x"
None#toFloat
toFloat : String -> Option Float
toFloat sThe floating-point number written in s, or None when s is not
one.
> toFloat "3.5"
Some 3.5
> toFloat "nope"
None#Searching
#startsWith
startsWith : String -> String -> Bool
startsWith prefix sWhether s begins with prefix.
> startsWith "he" "hello"
True
> startsWith "lo" "hello"
False#endsWith
endsWith : String -> String -> Bool
endsWith suffix sWhether s ends with suffix.
> endsWith "lo" "hello"
True#stripPrefix
stripPrefix : String -> String -> Option String
stripPrefix prefix ss without its leading prefix, or None when s does not begin
with it.
Unlike drop, the result says whether the prefix was there.
> stripPrefix "he" "hello"
Some "llo"
> stripPrefix "xy" "hello"
None#stripSuffix
stripSuffix : String -> String -> Option String
stripSuffix suffix ss without its trailing suffix, or None when s does not end with
it.
> stripSuffix "lo" "hello"
Some "hel"
> stripSuffix "xy" "hello"
None#contains
contains : String -> String -> Bool
contains needle haystackWhether needle occurs anywhere in haystack.
The empty string occurs in every string.
> contains "ell" "hello"
True
> contains "xyz" "hello"
False#indexOf
indexOf : String -> String -> Option Int
indexOf needle haystackThe position of the first occurrence of needle in haystack, or
None.
> indexOf "lo" "hello"
Some 3
> indexOf "z" "hello"
None#lastIndexOf
lastIndexOf : String -> String -> Option Int
lastIndexOf needle haystackThe position of the last occurrence of needle in haystack, or
None.
Occurrences may overlap. An empty needle is found at the end of the string.
> lastIndexOf "l" "hello"
Some 3
> lastIndexOf "z" "hello"
None#countOccurrences
countOccurrences : String -> String -> Int
countOccurrences needle haystackThe number of non-overlapping occurrences of needle in haystack.
0 for an empty needle.
> countOccurrences "l" "hello"
2
> countOccurrences "ll" "lllll"
2#Building
#prepend
prepend : String -> String -> String
prepend pre spre followed by s.
> prepend "un" "do"
"undo"#concat
concat : List String -> String
concat partsThe strings joined end to end.
> concat ["a", "bc", "d"]
"abcd"#join
join : String -> List String -> String
join sep partsThe strings joined with sep between each adjacent pair.
> join ", " ["a", "b", "c"]
"a, b, c"#repeat
repeat : Int -> String -> String
repeat n ss repeated n times.
Empty when n <= 0. Safe for large n: the call depth grows with
log n, not n.
> repeat 3 "ab"
"ababab"#Transformation
#reverse
reverse : String -> String
reverse sThe string with its characters in reverse order.
> reverse "abc"
"cba"#trimLeft
trimLeft : String -> String
trimLeft sThe string without its leading whitespace.
> trimLeft " hi "
"hi "#trimRight
trimRight : String -> String
trimRight sThe string without its trailing whitespace.
> trimRight " hi "
" hi"#trim
trim : String -> String
trim sThe string without leading or trailing whitespace.
> trim " hi "
"hi"#toUpper
toUpper : String -> String
toUpper sThe string with every ASCII letter in uppercase.
Other characters are unchanged, so ß stays ß.
> toUpper "Straße"
"STRAßE"#toLower
toLower : String -> String
toLower sThe string with every ASCII letter in lowercase.
Other characters are unchanged.
> toLower "HÉLLO"
"hÉllo"#capitalize
capitalize : String -> String
capitalize sThe string with its first character in uppercase.
> capitalize "hello"
"Hello"#replace
replace : String -> String -> String -> String
replace old new sThe string with the first occurrence of old replaced by new.
Unchanged when old is absent or empty.
> replace "l" "L" "hello"
"heLlo"#replaceAll
replaceAll : String -> String -> String -> String
replaceAll old new sThe string with every non-overlapping occurrence of old replaced by
new.
Unchanged when old is empty.
> replaceAll "l" "L" "hello"
"heLLo"#Slicing and splitting
#sliceClamped
sliceClamped : Int -> Int -> String -> String
sliceClamped lo hi sThe characters at positions [lo, hi).
Positions are clamped to the string, so an out-of-range slice is shorter
rather than a panic. s.[lo..hi] is the panicking form.
> sliceClamped 1 4 "hello"
"ell"#take
take : Int -> String -> String
take n sThe first n characters, or the whole string when it is shorter.
> take 3 "hello"
"hel"#drop
drop : Int -> String -> String
drop n sEverything after the first n characters.
> drop 3 "hello"
"lo"#splitAt
splitAt : Int -> String -> (String, String)
splitAt n sThe first n characters, and the rest.
> splitAt 2 "hello"
("he", "llo")#split
split : String -> String -> List String
split sep sThe pieces of s between occurrences of sep, with the separators
removed.
An empty separator yields the whole string as the only piece.
> split "," "a,b,c"
["a", "b", "c"]
> split "," "abc"
["abc"]#lines
lines : String -> List String
lines sThe lines of s, split on \n.
A \r before the \n is removed, so Windows line endings work too.
> lines "a\nb\nc"
["a", "b", "c"]#stripCR
stripCR : String -> String
stripCR lineThe line without one trailing \r.
Unchanged when there is none.
> stripCR "ab\r"
"ab"
> stripCR "ab"
"ab"#words
words : String -> List String
words sThe words of s: the runs of characters between whitespace.
Leading, trailing, and repeated whitespace produce no empty words.
> words " hello world "
["hello", "world"]#unlines
unlines : List String -> String
unlines partsThe lines joined with \n, with a newline after each one.
> unlines ["a", "b"]
"a\nb\n"#unwords
unwords : List String -> String
unwords partsThe words joined with single spaces.
> unwords ["a", "b", "c"]
"a b c"#Padding
#padLeft
padLeft : Int -> Char -> String -> String
padLeft n c sThe string padded on the left with c to length n.
Unchanged when it is already at least n long.
> padLeft 5 '.' "ab"
"...ab"#padRight
padRight : Int -> Char -> String -> String
padRight n c sThe string padded on the right with c to length n.
Unchanged when it is already at least n long.
> padRight 5 '.' "ab"
"ab..."#center
center : Int -> Char -> String -> String
center n c sThe string centered in a field of width n, padded with c.
When the padding is odd, the extra character goes on the right.
Unchanged when the string is already at least n long.
> center 5 '.' "ab"
".ab.."#Instances
#Index String Int Char
impl Index String Int CharThe character at a codepoint position: s[i].
Panics with an index error when the position is out of range. Positions
count codepoints, matching string.toChars.
#Slice String
impl Slice StringThe substring over codepoint positions [lo, hi).
Panics with a slice error when the range runs outside the string;
string.sliceClamped clamps instead.
> slice "hello" 1 4
"ell"