Maintainer | dneavesdev@pm.me |
---|---|
Safe Haskell | Safe |
Language | GHC2021 |
Antelude.String
Description
Synopsis
- type String = [Char]
- class IsString a where
- fromString :: String -> a
- lines :: String -> [String]
- unlines :: [String] -> String
- unwords :: [String] -> String
- words :: String -> [String]
- bytestring :: String -> ByteString
- bytestringLazy :: String -> ByteStringLazy
- contains :: String -> String -> Bool
- join :: List String -> String
- joinWith :: String -> List String -> String
- pad :: Int -> Char -> String -> String
- padLeft :: Int -> Char -> String -> String
- padRight :: Int -> Char -> String -> String
- replace :: String -> String -> String -> String
- split :: String -> String -> List String
- text :: String -> Text
- textLazy :: String -> TextLazy
- trim :: Char -> String -> String
- trimLeft :: Char -> String -> String
- trimRight :: Char -> String -> String
- unBytestring :: ByteString -> String
- unBytestringLazy :: ByteStringLazy -> String
- unText :: Text -> String
- unTextLazy :: TextLazy -> String
Documentation
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.
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.
Methods
fromString :: String -> a #
Instances
IsString ByteString | Beware: |
Defined in Data.ByteString.Internal.Type Methods fromString :: String -> ByteString # | |
IsString ByteString | Beware: |
Defined in Data.ByteString.Lazy.Internal Methods fromString :: String -> ByteString # | |
IsString Doc | |
Defined in Text.PrettyPrint.HughesPJ Methods fromString :: String -> Doc # | |
IsString a => IsString (Identity a) | Since: base-4.9.0.0 |
Defined in Data.String Methods fromString :: String -> Identity a # | |
a ~ Char => IsString (Seq a) | Since: containers-0.5.7 |
Defined in Data.Sequence.Internal Methods fromString :: String -> Seq a # | |
IsString (Doc a) | |
Defined in Text.PrettyPrint.Annotated.HughesPJ Methods fromString :: String -> Doc a # | |
a ~ Char => IsString [a] |
Since: base-2.1 |
Defined in Data.String Methods fromString :: String -> [a] # | |
IsString a => IsString (Const a b) | Since: base-4.9.0.0 |
Defined in Data.String Methods fromString :: String -> Const a b # |
Reexport from String
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)
Reexport from String
Reexport from String
Reexport from String
bytestring :: String -> ByteString Source #
Turn a String into a strict ByteString
bytestringLazy :: String -> ByteStringLazy Source #
Turn a String into a lazy ByteString
contains :: String -> String -> Bool Source #
Given a substring, see if it is within a provided String
.
padRight :: Int -> Char -> String -> String Source #
Add a specified number of Char
s to the right side
replace :: String -> String -> String -> String Source #
Given a "replaceable" substring to replace, and a "replacement" substring, replace all instances of the "replaceable" within a String
with the "replacement".
unBytestring :: ByteString -> String Source #
Turn a strict ByteString into a String
unBytestringLazy :: ByteStringLazy -> String Source #
Turn a lazy ByteString into a String
unTextLazy :: TextLazy -> String Source #
Turn a lazy Text into a String