String Functions in Haskell
In Haskell, string manipulation is typically done using the Data.Text
module, which provides efficient string handling. We’ll use this module along with some functions from the standard Prelude for our examples.
This code demonstrates various string operations using the Data.Text
module in Haskell. Here’s a breakdown of the functions used:
isInfixOf
: Checks if one string is a substring of another.count
: Counts occurrences of a substring.isPrefixOf
andisSuffixOf
: Check for prefix and suffix.findIndex
: Finds the index of a character.intercalate
: Joins strings with a separator.replicate
: Repeats a string multiple times.replace
: Replaces all occurrences of a substring.splitOn
: Splits a string on a delimiter.toLower
andtoUpper
: Convert to lowercase and uppercase.
Note that in Haskell, we need to use pack
to convert string literals to Text
, and unpack
to convert Text
back to String
for printing.
To run this program, save it as string_functions.hs
and use:
This example demonstrates how to perform common string operations in Haskell using the Data.Text
module, which provides efficient string handling capabilities.