Portability | portable |
---|---|
Stability | provisional |
Maintainer | John Goerzen <jgoerzen@complete.org> |
Safe Haskell | None |
This module provides various helpful utilities for dealing with strings.
Written by John Goerzen, jgoerzen@complete.org
- strip :: String -> String
- lstrip :: String -> String
- rstrip :: String -> String
- startswith :: Eq a => [a] -> [a] -> Bool
- endswith :: Eq a => [a] -> [a] -> Bool
- join :: [a] -> [[a]] -> [a]
- split :: Eq a => [a] -> [a] -> [[a]]
- splitWs :: String -> [String]
- replace :: Eq a => [a] -> [a] -> [a] -> [a]
- escapeRe :: String -> String
- maybeRead :: Read a => String -> Maybe a
Whitespace Removal
strip :: String -> StringSource
Removes any whitespace characters that are present at the start or end of a string. Does not alter the internal contents of a string. If no whitespace characters are present at the start or end of a string, returns the original string unmodified. Safe to use on any string.
Note that this may differ from some other similar functions from other authors in that:
- If multiple whitespace characters are present all in a row, they are all removed;
- If no whitespace characters are present, nothing is done.
Tests
Note: These functions are aliases for functions in Data.List.Utils.
startswith :: Eq a => [a] -> [a] -> BoolSource
Returns true if the given list starts with the specified elements; false otherwise. (This is an alias for Data.List.isPrefixOf.)
Example:
startswith "He" "Hello" -> True
endswith :: Eq a => [a] -> [a] -> BoolSource
Returns true if the given list ends with the specified elements; false otherwise. (This is an alias for Data.List.isSuffixOf.)
Example:
endswith "lo" "Hello" -> True
Conversions
Note: Some of these functions are aliases for functions in Data.List.Utils.
join :: [a] -> [[a]] -> [a]Source
Given a delimiter and a list of items (or strings), join the items by using the delimiter.
Example:
join "|" ["foo", "bar", "baz"] -> "foo|bar|baz"
split :: Eq a => [a] -> [a] -> [[a]]Source
Given a delimiter and a list (or string), split into components.
Example:
split "," "foo,bar,,baz," -> ["foo", "bar", "", "baz", ""]
split "ba" ",foo,bar,,baz," -> [",foo,","r,,","z,"]
splitWs :: String -> [String]Source
Splits a string around whitespace. Empty elements in the result list are automatically removed.
replace :: Eq a => [a] -> [a] -> [a] -> [a]Source
Given a list and a replacement list, replaces each occurance of the search list with the replacement list in the operation list.
Example:
replace "," "." "127,0,0,1" -> "127.0.0.1"
This could logically be thought of as:
replace old new l = join new . split old $ l
escapeRe :: String -> StringSource
Escape all characters in the input pattern that are not alphanumeric.
Does not make special allowances for NULL, which isn't valid in a Haskell regular expression pattern.