Haskell Language Indexing Text


Example

{-# 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) and Text and returns the index of the first occurrence of a given string or Nothing if it doesn't occur.

ghci> T.findIndex ('s'==) myText
Just 2
ghci> T.findIndex ('c'==) myText
Nothing

The count function returns the number of times a query Text occurs within another Text.

ghci> count ("miss"::T.Text) myText
1