On this page
Data.String
| Copyright | (c) The University of Glasgow 2007 |
|---|---|
| License | BSD-style (see the file libraries/base/LICENSE) |
| Maintainer | libraries@haskell.org |
| Stability | stable |
| Portability | portable |
| Safe Haskell | Trustworthy |
| Language | Haskell2010 |
Contents
Description
The String type and associated operations.
A String is a list of characters. String constants in Haskell are values of type String.
See Data.List for operations on lists.
Class for string-like datastructures; used by the overloaded string extension (-XOverloadedStrings in GHC).
Methods
fromString :: String -> a Source
Instances
| IsString a => IsString (Identity a) Source | Since: base-4.9.0.0 |
|
Defined in Data.String MethodsfromString :: String -> Identity a Source |
|
| a ~ Char => IsString [a] Source |
Since: base-2.1 |
|
Defined in Data.String MethodsfromString :: String -> [a] Source |
|
| IsString a => IsString (Const a b) Source | Since: base-4.9.0.0 |
|
Defined in Data.String MethodsfromString :: String -> Const a b Source |
|
Functions on strings
lines :: String -> [String] Source
Splits the argument into a list of lines stripped of their terminating n characters. The n terminator is optional in a final non-empty line of the argument string.
For example:
>>> lines "" -- empty input contains no lines
[]
>>> lines "\n" -- single empty line
[""]
>>> lines "one" -- single unterminated line
["one"]
>>> lines "one\n" -- single non-empty line
["one"]
>>> lines "one\n\n" -- second line is empty
["one",""]
>>> lines "one\ntwo" -- second line is unterminated
["one","two"]
>>> lines "one\ntwo\n" -- two non-empty lines
["one","two"]
When the argument string is empty, or ends in a n character, it can be recovered by passing the result of lines to the unlines function. Otherwise, unlines appends the missing terminating n. This makes unlines . lines idempotent:
(unlines . lines) . (unlines . lines) = (unlines . lines)
words :: String -> [String] Source
words breaks a string up into a list of words, which were delimited by white space.
>>> words "Lorem ipsum\ndolor"
["Lorem","ipsum","dolor"]
unlines :: [String] -> String Source
Appends a n character to each input string, then concatenates the results. Equivalent to foldMap (s -> s ++ "n").
>>> unlines ["Hello", "World", "!"]
"Hello\nWorld\n!\n"
Note
unlines . lines /= id when the input is not n-terminated:
>>> unlines . lines $ "foo\nbar"
"foo\nbar\n"
unwords :: [String] -> String Source
unwords is an inverse operation to words. It joins words with separating spaces.
>>> unwords ["Lorem", "ipsum", "dolor"]
"Lorem ipsum dolor"
© The University of Glasgow and others
Licensed under a BSD-style license (see top of the page).
https://downloads.haskell.org/~ghc/9.4.2/docs/libraries/base-4.17.0.0/Data-String.html