string package:base

Parses and returns the specified string.
String is an alias for a list of characters. String constants in Haskell are values of type 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 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

[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 (++) (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 the bytestring package for binary data.
The String type and associated operations.
Utilities for primitive marshalling of C strings. The marshalling converts each Haskell character, representing a Unicode code point, to one or more bytes in a manner that, by default, is determined by the current locale. As a consequence, no guarantees can be made about the relative length of a Haskell string and its corresponding C string, and therefore all the marshalling routines include memory allocation. The translation between Unicode and the encoding of the current locale may be lossy.
String literal, with escapes interpreted
utility function converting a String to a show function that simply prepends the string unchanged.
IsString 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 apply.
A C string is a reference to an array of C characters terminated by NUL.
A string with explicit length information in bytes instead of a terminating NUL (allowing NUL characters in the middle of the string).
A C wide string is a reference to an array of C wide characters terminated by NUL.
A wide character string with explicit length information in CWchars instead of a terminating NUL (allowing NUL characters in the middle of the string).
Marshal a Haskell string into a NUL terminated C string.
  • the Haskell string may not contain any NUL characters
  • new storage is allocated for the C string and must be explicitly freed using free or finalizerFree.
Marshal a Haskell string into a C string (ie, character array) with explicit length information.
  • new storage is allocated for the C string and must be explicitly freed using free or finalizerFree.
Marshal a Haskell string into a NUL terminated C string.
  • the Haskell string may not contain any NUL characters
  • new storage is allocated for the C string and must be explicitly freed using free or finalizerFree.
Marshal a Haskell string into a C string (ie, character array) with explicit length information.
  • new storage is allocated for the C string and must be explicitly freed using free or finalizerFree.
Marshal a Haskell string into a NUL terminated C wide string.
  • the Haskell string may not contain any NUL characters
  • new storage is allocated for the C wide string and must be explicitly freed using free or finalizerFree.
Marshal a Haskell string into a C wide string (ie, wide character array) with explicit length information.
  • new storage is allocated for the C wide string and must be explicitly freed using free or finalizerFree.
Marshal a NUL terminated C string into a Haskell string.
Marshal a C string with explicit length into a Haskell string.
Marshal a NUL terminated C string into a Haskell string.
Marshal a C string with explicit length into a Haskell string.
Marshal a NUL terminated C wide string into a Haskell string.
Marshal a C wide string with explicit length into a Haskell string.
Marshal a Haskell string into a NUL terminated C string using temporary storage.
  • the Haskell string may not contain any NUL characters
  • the memory is freed when the subcomputation terminates (either normally or via an exception), so the pointer to the temporary storage must not be used after this.