base- Basic libraries
Copyright(c) The University of Glasgow 2007
LicenseBSD-style (see the file libraries/base/LICENSE)
Safe HaskellTrustworthy



The String type and associated operations.



type String = [Char] Source #

A String is a list of characters. String constants in Haskell are values of type String.

See Data.List for operations on lists.

class IsString a where Source #

Class for string-like datastructures; used by the overloaded string extension (-XOverloadedStrings in GHC).


fromString :: String -> a Source #


Instances details
IsString a => IsString (Identity a) Source #

Since: base-

Instance details

Defined in Data.String

a ~ Char => IsString [a] Source #

(a ~ Char) context was introduced in

Since: base-2.1

Instance details

Defined in Data.String


fromString :: String -> [a] Source #

IsString a => IsString (Const a b) Source #

Since: base-

Instance details

Defined in Data.String


fromString :: 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
>>> lines "one\n"      -- single non-empty line
>>> lines "one\n\n"    -- second line is empty
>>> lines "one\ntwo"   -- second line is unterminated
>>> lines "one\ntwo\n" -- two non-empty lines

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 (as defined by isSpace). This function trims any white spaces at the beginning and at the end.

>>> words "Lorem ipsum\ndolor"
>>> words " foo bar "

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", "!"]

Note that unlines . lines /= id when the input is not \n-terminated:

>>> unlines . lines $ "foo\nbar"

unwords :: [String] -> String Source #

unwords joins words with separating spaces (U+0020 SPACE).

>>> unwords ["Lorem", "ipsum", "dolor"]
"Lorem ipsum dolor"

unwords is neither left nor right inverse of words:

>>> words (unwords [" "])
>>> unwords (words "foo\nbar")
"foo bar"