Data.String (original) (raw)

Contents

Description

The String type and associated operations.

Synopsis

Documentation

type String = [Char] Source #

[String](Data-String.html#t:String "Data.String") is an alias for a list of characters.

String constants in Haskell are values of type [String](Data-String.html#t:String "Data.String"). That means if you write a string literal like "hello world", it will have the type [Char], which is the same as String.

Note: You can ask the compiler to automatically infer different types with the -XOverloadedStrings language extension, for example"hello world" :: Text. See [IsString](Data-String.html#t:IsString "Data.String") for more information.

Because String is just a list of characters, you can use normal list functions to do basic string manipulation. See Data.List for operations on lists.

Performance considerations

Expand

[Char] is a relatively memory-inefficient type. It is a linked list of boxed word-size characters, internally it looks something like:

╭─────┬───┬──╮ ╭─────┬───┬──╮ ╭─────┬───┬──╮ ╭────╮ │ (:) │ │ ─┼─>│ (:) │ │ ─┼─>│ (:) │ │ ─┼─>│ [] │ ╰─────┴─┼─┴──╯ ╰─────┴─┼─┴──╯ ╰─────┴─┼─┴──╯ ╰────╯ v v v 'a' 'b' 'c'

The String "abc" will use 5*3+1 = 16 (in general 5n+1) words of space in memory.

Furthermore, operations like [(++)](GHC-List.html#v:-43--43- "GHC.List") (string concatenation) are O(n) (in the left argument).

For historical reasons, the base library uses String in a lot of places for the conceptual simplicity, but library code dealing with user-data should use the text package for Unicode text, or the thebytestring package for binary data.

class IsString a where Source #

[IsString](Data-String.html#t:IsString "Data.String") is used in combination with the -XOverloadedStrings language extension to convert the literals to different string types.

For example, if you use the text package, you can say

{-# LANGUAGE OverloadedStrings #-}

myText = "hello world" :: Text

Internally, the extension will convert this to the equivalent of

myText = fromString @Text ("hello world" :: String)

Note: You can use fromString in normal code as well, but the usual performance/memory efficiency problems with [String](Data-String.html#t:String "Data.String") apply.

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.

When the argument string is empty, or ends in a \n character, it can be recovered by passing the result of [lines](Data-String.html#v:lines "Data.String") to the [unlines](Data-String.html#v:unlines "Data.String") function. Otherwise, [unlines](Data-String.html#v:unlines "Data.String") appends the missing terminating \n. This makesunlines . lines idempotent:

(unlines . lines) . (unlines . lines) = (unlines . lines)

Examples

Expand

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

words :: String -> [String] Source #

[words](Data-String.html#v:words "Data.String") breaks a string up into a list of words, which were delimited by white space (as defined by [isSpace](Data-Char.html#v:isSpace "Data.Char")). This function trims any white spaces at the beginning and at the end.

Examples

Expand

>>> words "Lorem ipsum\ndolor"** **["Lorem","ipsum","dolor"]

>>> words " foo bar "** **["foo","bar"]

unlines :: [String] -> String Source #

Appends a \n character to each input string, then concatenates the results. Equivalent to `foldMap` (s -> s `[++](GHC-List.html#v:-43--43- "GHC.List")` "\n").

Examples

Expand

>>> unlines ["Hello", "World", "!"]** **"Hello\nWorld\n!\n"

Note that `[unlines](Data-String.html#v:unlines "Data.String")` `[.](Data-Function.html#v:. "Data.Function")` `[lines](Data-String.html#v:lines "Data.String")` `[/=](Data-Eq.html#v:-47--61- "Data.Eq")` `[id](Data-Function.html#v:id "Data.Function")` when the input is not \n-terminated:

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

unwords :: [String] -> String Source #

[unwords](Data-String.html#v:unwords "Data.String") joins words with separating spaces (U+0020 SPACE).

[unwords](Data-String.html#v:unwords "Data.String") is neither left nor right inverse of [words](Data-String.html#v:words "Data.String"):

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

Examples

Expand

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

>>> unwords ["foo", "bar", "", "baz"]** **"foo bar baz"