Tutorial by Examples

The OverloadedStrings language extension allows the use of normal string literals to stand for Text values. {-# LANGUAGE OverloadedStrings #-} import qualified Data.Text as T myText :: T.Text myText = "overloaded"
{-# LANGUAGE OverloadedStrings #-} import qualified Data.Text as T myText :: T.Text myText = "\n\r\t leading and trailing whitespace \t\r\n" strip removes whitespace from the start and end of a Text value. ghci> T.strip myText "leading and trailing whitespace" ...
{-# LANGUAGE OverloadedStrings #-} import qualified Data.Text as T myText :: T.Text myText = "mississippi" splitOn breaks a Text up into a list of Texts on occurrences of a substring. ghci> T.splitOn "ss" myText ["mi","i","ippi"] sp...
Encoding and decoding functions for a variety of Unicode encodings can be found in the Data.Text.Encoding module. ghci> import Data.Text.Encoding ghci> decodeUtf8 (encodeUtf8 "my text") "my text" Note that decodeUtf8 will throw an exception on invalid input. If you wa...
ghci> :set -XOverloadedStrings ghci> import Data.Text as T isInfixOf :: Text -> Text -> Bool checks whether a Text is contained anywhere within another Text. ghci> "rum" `T.isInfixOf` "crumble" True isPrefixOf :: Text -> Text -> Bool checks whether a...
{-# LANGUAGE OverloadedStrings #-} import qualified Data.Text as T myText :: T.Text myText = "mississippi" Characters at specific indices can be returned by the index function. ghci> T.index myText 2 's' The findIndex function takes a function of type (Char -> Bool) ...

Page 1 of 1