2{-| A built-in representation for efficient string manipulation. String literals
3are enclosed in `"double quotes"`. Strings are *not* lists of characters.
6@docs isEmpty, length, reverse, repeat
8# Building and Splitting
9@docs cons, uncons, append, concat, split, join, words, lines
12@docs sub, left, right, dropLeft, dropRight
15@docs contains, startsWith, endsWith, indexes, indices
18@docs show, toInt, toFloat, toList, fromList
21Cosmetic operations such as padding with extra characters or trimming whitespace.
23@docs toUpper, toLower,
24 pad, padLeft, padRight,
25 trim, trimLeft, trimRight
27# Higher-Order Functions
28@docs map, filter, foldl, foldr, any, all
35{-| Check if a string is empty.
38 isEmpty "the world" == False
40isEmpty : String -> Bool
41isEmpty = Native.String.isEmpty
43{-| Add a character to the beginning of a string -}
44cons : Char -> String -> String
45cons = Native.String.cons
47{-| Split a non-empty string into its head and tail. This lets you
48pattern match on strings exactly as you would with lists.
50 uncons "abc" == Just ('a',"bc")
53uncons : String -> Maybe (Char, String)
54uncons = Native.String.uncons
56{-| Append two strings. You can also use [the `(++)` operator](/library/List.elm#++)
59 append "butter" "fly" == "butterfly"
61append : String -> String -> String
62append = Native.String.append
64{-| Concatenate many strings into one.
66 concat ["never","the","less"] == "nevertheless"
68concat : [String] -> String
69concat = Native.String.concat
71{-| Get the length of a string.
73 length "innumerable" == 11
77length = Native.String.length
79{-| Transform every character in a string
81 map (\c -> if c == '/' then '.' else c) "a/b/c" == "a.b.c"
83map : (Char -> Char) -> String -> String
84map = Native.String.map
86{-| Keep only the characters that satisfy the predicate:
88 filter isDigit "R2-D2" == "22"
90filter : (Char -> Bool) -> String -> String
91filter = Native.String.filter
95 reverse "stressed" == "desserts"
97reverse : String -> String
98reverse = Native.String.reverse
100{-| Reduce a string from the left:
102 foldl cons "" "time" == "emit"
104foldl : (Char -> b -> b) -> b -> String -> b
105foldl = Native.String.foldl
107{-| Reduce a string from the right:
109 foldr cons "" "time" == "time"
111foldr : (Char -> b -> b) -> b -> String -> b
112foldr = Native.String.foldr
114{-| Split a string using a given separator.
116 split "," "cat,dog,cow" == ["cat","dog","cow"]
117 split "/" "home/evan/Desktop/" == ["home","evan","Desktop"]
119Use `Regex.split` if you need something more flexible.
121split : String -> String -> [String]
122split = Native.String.split
124{-| Put many strings together with a given separator.
126 join " " ["cat","dog","cow"] == "cat dog cow"
127 join "/" ["home","evan","Desktop"] == "home/evan/Desktop"
129join : String -> [String] -> String
130join = Native.String.join
132{-| Repeat a string N times.
134 repeat 3 "ha" == "hahaha"
136repeat : Int -> String -> String
137repeat = Native.String.repeat
139{-| Take a substring given a start and end index. Negative indexes
140are taken starting from the *end* of the list.
142 sub 7 9 "snakes on a plane!" == "on"
143 sub 0 6 "snakes on a plane!" == "snakes"
144 sub 0 -7 "snakes on a plane!" == "snakes on a"
145 sub -6 -1 "snakes on a plane!" == "plane"
147sub : Int -> Int -> String -> String
148sub = Native.String.sub
150{-| Take N characters from the left side of a string. -}
151left : Int -> String -> String
152left = Native.String.left
154{-| Take N characters from the right side of a string. -}
155right : Int -> String -> String
156right = Native.String.right
158{-| Drop N characters from the left side of a string. -}
159dropLeft : Int -> String -> String
160dropLeft = Native.String.dropLeft
162{-| Drop N characters from the right side of a string. -}
163dropRight : Int -> String -> String
164dropRight = Native.String.dropRight
166{-| Pad a string on both sides until it has a given length.
168 pad 5 ' ' "1" == " 1 "
169 pad 5 ' ' "11" == " 11 "
170 pad 5 ' ' "121" == " 121 "
172pad : Int -> Char -> String -> String
173pad = Native.String.pad
175{-| Pad a string on the left until it has a given length.
177 padLeft 5 '.' "1" == "....1"
178 padLeft 5 '.' "11" == "...11"
179 padLeft 5 '.' "121" == "..121"
181padLeft : Int -> Char -> String -> String
182padLeft = Native.String.padLeft
184{-| Pad a string on the right until it has a given length.
186 padRight 5 '.' "1" == "1...."
187 padRight 5 '.' "11" == "11..."
188 padRight 5 '.' "121" == "121.."
190padRight : Int -> Char -> String -> String
191padRight = Native.String.padRight
193{-| Get rid of whitespace on both sides of a string.
195 trim " hats \n" == "hats"
197trim : String -> String
198trim = Native.String.trim
200{-| Get rid of whitespace on the left of a string.
202 trimLeft " hats \n" == "hats \n"
204trimLeft : String -> String
205trimLeft = Native.String.trimLeft
207{-| Get rid of whitespace on the left of a string.
209 trimRight " hats \n" == " hats"
211trimRight : String -> String
212trimRight = Native.String.trimRight
214{-| Break a string into words, splitting on chunks of whitespace.
216 words "How are \t you? \n Good?" == ["How","are","you?","Good?"]
218words : String -> [String]
219words = Native.String.words
221{-| Break a string into lines, splitting on newlines.
223 lines "How are you?\nGood? == ["How are you?", "Good?"]
225lines : String -> [String]
226lines = Native.String.lines
228{-| Convert a string to all upper case. Useful for case insensitive comparisons
231toUpper : String -> String
232toUpper = Native.String.toUpper
234{-| Convert a string to all lower case. Useful for case insensitive comparisons. -}
235toLower : String -> String
236toLower = Native.String.toLower
238{-| Check to see if *any* characters satisfy a predicate.
240 any isDigit "90210" == True
241 any isDigit "R2-D2" == True
242 any isDigit "heart" == False
244any : (Char -> Bool) -> String -> Bool
245any = Native.String.any
247{-| Check to see if *all* characters satisfy a predicate.
249 all isDigit "90210" == True
250 all isDigit "R2-D2" == False
251 all isDigit "heart" == False
253all : (Char -> Bool) -> String -> Bool
254all = Native.String.all
256{-| See if a string contains another one.
258 contains "the" "theory" == True
259 contains "hat" "theory" == False
260 contains "THE" "theory" == False
262Use `Regex.contains` if you need something more flexible.
264contains : String -> String -> Bool
265contains = Native.String.contains
267{-| See if a string starts with another one.
269 startsWith "the" "theory" == True
270 startsWith "ory" "theory" == False
272startsWith : String -> String -> Bool
273startsWith = Native.String.startsWith
275{-| See if a string ends with another one.
277 endsWith "the" "theory" == False
278 endsWith "ory" "theory" == True
280endsWith : String -> String -> Bool
281endsWith = Native.String.endsWith
283{-| Get all of the indexes for a substring.
285 indexes "i" "Mississippi" == [1,4,7,10]
286 indexes "ss" "Mississippi" == [2,5]
287 indexes "needle" "haystack" == []
289indexes : String -> String -> [Int]
290indexes = Native.String.indexes
292{-| Alias for `indexes` -}
293indices : String -> String -> [Int]
294indices = Native.String.indexes
296{-| Turn any kind of value into a string.
299 show [1,2] == "[1,2]"
302show = Native.Show.show
304{-| Try to convert a string into an int, failing on improperly formatted strings.
306 toInt "123" == Just 123
307 toInt "-42" == Just -42
308 toInt "3.1" == Nothing
309 toInt "31a" == Nothing
311toInt : String -> Maybe Int
312toInt = Native.String.toInt
314{-| Try to convert a string into a float, failing on improperly formatted strings.
316 toFloat "123" == Just 123.0
317 toFloat "-42" == Just -42.0
318 toFloat "3.1" == Just 3.1
319 toFloat "31a" == Nothing
321toFloat : String -> Maybe Float
322toFloat = Native.String.toFloat
324{-| Convert a string to a list of characters.
326 toList "abc" == ['a','b','c']
328toList : String -> [Char]
329toList = Native.String.toList
331{-| Convert a list of characters into a String. Can be useful if you
332want to create a string primarly by consing, perhaps for decoding
335 fromList ['a','b','c'] == "abc"
337fromList : [Char] -> String
338fromList = Native.String.fromList
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list