| Portability | portable |
|---|---|
| Stability | provisional |
| Maintainer | Julian Fleischer <julian.fleischer@fu-berlin.de> |
| Safe Haskell | Trustworthy |
Data.Strings
Description
Functions for working with strings, including Text, ByteString, etc.
This module aims at offering a consistent interface across all the available
string types. It currently offers instances for the ordinary Haskell String
type, Text, lazy Text, ByteString, and lazy ByteString.
It especially provides functions for some types, which are otherwise not
available nativly (such as breakOnSubstring which is not available for the
lazy Text type, is offered by sBreak and strBreak).
- text :: String -> Text
- lazyText :: String -> Text
- bytes :: String -> ByteString
- lazyBytes :: String -> ByteString
- charToByte :: Char -> Word8
- byteToChar :: Word8 -> Char
- class (Eq a, Strings a) => Str a where
- strNull :: a -> Bool
- strLen :: a -> Int
- strHead :: a -> Char
- strLast :: a -> Char
- strInit :: a -> a
- strTail :: a -> a
- strTake :: Int -> a -> a
- strDrop :: Int -> a -> a
- strReplace :: String -> String -> a -> a
- strBreak :: String -> a -> (a, a)
- strSplit :: String -> a -> (a, a)
- strSplitAll :: String -> a -> [a]
- strToUpper :: a -> a
- strToLower :: a -> a
- strCapitalize :: a -> a
- strMap :: (Char -> Char) -> a -> a
- strConcat :: [a] -> a
- strJoin :: String -> [a] -> a
- strAppend :: String -> a -> a
- strCons :: Char -> a -> a
- strTrim :: a -> a
- strPadLeft :: Char -> Int -> a -> a
- strPadRight :: Char -> Int -> a -> a
- strPadBoth :: Char -> Int -> a -> a
- strReverse :: a -> a
- strEq :: String -> a -> Bool
- strStartsWith :: a -> String -> Bool
- strEndsWith :: a -> String -> Bool
- fromString :: String -> a
- fromUnicodeString :: String -> a
- toString :: a -> String
- toWord8 :: a -> [Word8]
- class Strings a where
- sNull :: a -> Bool
- sEmpty :: a
- sLen :: a -> Int
- sHead :: a -> Char
- sLast :: a -> Char
- sInit :: a -> a
- sTail :: a -> a
- sTake :: Int -> a -> a
- sDrop :: Int -> a -> a
- sTakeWhile :: (Char -> Bool) -> a -> a
- sDropWhile :: (Char -> Bool) -> a -> a
- sReplace :: a -> a -> a -> a
- sBreak :: a -> a -> (a, a)
- sSplit :: a -> a -> (a, a, Bool)
- sSplitAll :: a -> a -> [a]
- sStartsWith :: a -> a -> Bool
- sEndsWith :: a -> a -> Bool
- sCons :: Char -> a -> a
- sCapitalize :: a -> a
- sMap :: (Char -> Char) -> a -> a
- sConcat :: [a] -> a
- sTrim :: a -> a
- sPadLeft :: Char -> Int -> a -> a
- sPadRight :: Char -> Int -> a -> a
- sPadBoth :: Char -> Int -> a -> a
- sReverse :: a -> a
- sFromString :: String -> a
- sFromUnicodeString :: String -> a
- sToString :: a -> String
- sToWord8 :: a -> [Word8]
Documentation
bytes :: String -> ByteStringSource
Create a ByteString object form an ordinary Haskell String.
This function will encode a String using the UTF-8 character encoding.
lazyBytes :: String -> ByteStringSource
Create a lazy ByteString object from an ordinary Haskell String.
This function will encode a String using the UTF-8 character encoding.
class (Eq a, Strings a) => Str a whereSource
The Str class provides functions for working with arbitrary Strings.
It is basically the same interface as provided by the Strings class.
However, every input string is a Haskell String here, thus easing the
usage of different string types with native Haskell String literals.
For example strAppend suffix works with any string type for which
an instance of Str is defined. In order to maximize the ease of use
of this library, the functions are prefixed with str.
The complexity and efficiency of these functions depends on the underlying string type being used.
Minimal complete definition: It suffices to provide instances for
Eq and Strings.
Methods
Check whether the given string is empty or not. null generalised.
length generalised.
strTake :: Int -> a -> aSource
take generalised.
strDrop :: Int -> a -> aSource
drop generalised.
Arguments
| :: String | Needle. |
| -> String | Replacement. |
| -> a | Haystack. |
| -> a | Result: |
Replace a substring with another string.
strBreak :: String -> a -> (a, a)Source
Breaks the string on the first occurence of the given substring.
strBreak "xx" "1x2xx3xx4" = ("1x2", "xx3xx4")
strSplit :: String -> a -> (a, a)Source
Like strBreak, but the string to break on is excluded from the result.
strSplit "xx" "1x2xx3xx4" = ("1x2", "3xx4")
strSplitAll :: String -> a -> [a]Source
Split a string into multiple fragments, separated by the given substring.
strSplitAll "xx" "1x2xx3xx4" = ["1x2", "3", "4"]
strToUpper :: a -> aSource
Turn all characters in the string to upper case.
strToLower :: a -> aSource
Turn all characters in the string to lower case.
strCapitalize :: a -> aSource
Turn the first character in the string to upper case.
strMap :: (Char -> Char) -> a -> aSource
map generalised.
concat generalised.
strJoin :: String -> [a] -> aSource
Glue together multiple strings by a given Haskell String.
strJoin x = concat . intersperse x
strAppend :: String -> a -> aSource
Appends the given Haskell String to the string. ++ generalised.
strAppend " world" "hello" = "hello world"
strCons :: Char -> a -> aSource
Cons generalised.
Strips white space characters off both ends of the string.
strPadLeft :: Char -> Int -> a -> aSource
Appends the given character n times to the left, such that the resulting string has the given length.
strPadLeft '0' 8 "4711" == "00004711"
strPadRight :: Char -> Int -> a -> aSource
Appends the given character n times to the right, such that the resulting string has the given length.
strPadRight '0' 8 "4711" == "47110000"
strPadBoth :: Char -> Int -> a -> aSource
Appends the given character n times to both sides, such that the resulting string has the given length.
strPadBoth '0' 8 "4711" == "00471100"
strReverse :: a -> aSource
Reverse the string.
Check if the given Haskell String equals the string.
strStartsWith :: a -> String -> BoolSource
Check if the string starts with the given Haskell String.
strEndsWith :: a -> String -> BoolSource
Check if the string ends with the given Haskell String.
fromString :: String -> aSource
Create a string from a Haskell String.
fromUnicodeString :: String -> aSource
Create a string from a Haskell String. If the string does not support unicode, the Haskell String is encoded using UTF-8.
Convert the string into a Haskell String.
Convert the string into a list of bytes.
Instances
The goal of this class is to offer the same interface for various
types of strings (ByteString, Text, Haskell String, etc.).
If a certain type offers a native implementation for a given function,
Strings uses it. If not, a default implementation is given.
All of these functions are prefixed with s to avoid nameclashes
with existing functions from the prelude.
The complexity and efficiency of these functions depends on the underlying string type being used.
Methods
Check whether the given string is empty or not. null generalised.
The empty string.
length generalised.
take generalised.
drop generalised.
sTakeWhile :: (Char -> Bool) -> a -> aSource
takeWhile generalised.
sDropWhile :: (Char -> Bool) -> a -> aSource
dropWhile generalised.
sReplace :: a -> a -> a -> aSource
Replace a substring with another string.
sBreak :: a -> a -> (a, a)Source
Breaks the string on the first occurence of the given substring.
strBreak "xx" "1x2xx3xx4" = ("1x2", "xx3xx4")
sSplit :: a -> a -> (a, a, Bool)Source
Like sBreak, but the string to break on is excluded from the result.
strSplit "xx" "1x2xx3xx4" = ("1x2", "3xx4")
sSplitAll :: a -> a -> [a]Source
Split a string into multiple fragments, separated by the given substring.
strSplitAll "xx" "1x2xx3xx4" = ["1x2", "3", "4"]
sStartsWith :: a -> a -> BoolSource
Check if the string starts with the given string.
sEndsWith :: a -> a -> BoolSource
Check if the string ends with the given string.
Cons
sCapitalize :: a -> aSource
Turn the first character into an upper case character.
sMap :: (Char -> Char) -> a -> aSource
Map a function over all characters.
Concatenate all the strings in the list to a single string.
Strips white space characters off both ends of the string.
sPadLeft :: Char -> Int -> a -> aSource
Appends the given character n times to the left, such that the resulting string has the given length.
strPadLeft '0' 8 "4711" == "00004711"
sPadRight :: Char -> Int -> a -> aSource
Appends the given character n times to the right, such that the resulting string has the given length.
strPadRight '0' 8 "4711" == "47110000"
sPadBoth :: Char -> Int -> a -> aSource
Appends the given character n times to both sides, such that the resulting string has the given length.
strPadBoth '0' 8 "4711" == "00471100"
sFromString :: String -> aSource
Create a string from a Haskell String.
sFromUnicodeString :: String -> aSource
Create a string from a Haskell String. If the string does not support unicode, the Haskell String is encoded using UTF-8.
sToString :: a -> StringSource
Convert the string into a Haskell String.
sToWord8 :: a -> [Word8]Source
Convert the string into a list of bytes.
Instances