Module

Data.String.CodeUnits

#stripPrefix

stripPrefix :: Pattern -> String -> Maybe String

If the string starts with the given prefix, return the portion of the string left after removing it, as a Just value. Otherwise, return Nothing.

stripPrefix (Pattern "http:") "http://purescript.org" == Just "//purescript.org"
stripPrefix (Pattern "http:") "https://purescript.org" == Nothing

#stripSuffix

stripSuffix :: Pattern -> String -> Maybe String

If the string ends with the given suffix, return the portion of the string left after removing it, as a Just value. Otherwise, return Nothing.

stripSuffix (Pattern ".exe") "psc.exe" == Just "psc"
stripSuffix (Pattern ".exe") "psc" == Nothing

#contains

contains :: Pattern -> String -> Boolean

Checks whether the pattern appears in the given string.

contains (Pattern "needle") "haystack with needle" == true
contains (Pattern "needle") "haystack" == false

#singleton

singleton :: Char -> String

Returns a string of length 1 containing the given character.

singleton 'l' == "l"

#fromCharArray

fromCharArray :: Array Char -> String

Converts an array of characters into a string.

fromCharArray ['H', 'e', 'l', 'l', 'o'] == "Hello"

#toCharArray

toCharArray :: String -> Array Char

Converts the string into an array of characters.

toCharArray "Hello☺\n" == ['H','e','l','l','o','☺','\n']

#charAt

charAt :: Int -> String -> Maybe Char

Returns the character at the given index, if the index is within bounds.

charAt 2 "Hello" == Just 'l'
charAt 10 "Hello" == Nothing

#toChar

toChar :: String -> Maybe Char

Converts the string to a character, if the length of the string is exactly 1.

toChar "l" == Just 'l'
toChar "Hi" == Nothing -- since length is not 1

#uncons

uncons :: String -> Maybe { head :: Char, tail :: String }

Returns the first character and the rest of the string, if the string is not empty.

uncons "" == Nothing
uncons "Hello World" == Just { head: 'H', tail: "ello World" }

#length

length :: String -> Int

Returns the number of characters the string is composed of.

length "Hello World" == 11

#countPrefix

countPrefix :: (Char -> Boolean) -> String -> Int

Returns the number of contiguous characters at the beginning of the string for which the predicate holds.

countPrefix (_ /= ' ') "Hello World" == 5 -- since length "Hello" == 5

#indexOf

indexOf :: Pattern -> String -> Maybe Int

Returns the index of the first occurrence of the pattern in the given string. Returns Nothing if there is no match.

indexOf (Pattern "c") "abcdc" == Just 2
indexOf (Pattern "c") "aaa" == Nothing

#indexOf'

indexOf' :: Pattern -> Int -> String -> Maybe Int

Returns the index of the first occurrence of the pattern in the given string, starting at the specified index. Returns Nothing if there is no match.

indexOf' (Pattern "a") 2 "ababa" == Just 2
indexOf' (Pattern "a") 3 "ababa" == Just 4

#lastIndexOf

lastIndexOf :: Pattern -> String -> Maybe Int

Returns the index of the last occurrence of the pattern in the given string. Returns Nothing if there is no match.

lastIndexOf (Pattern "c") "abcdc" == Just 4
lastIndexOf (Pattern "c") "aaa" == Nothing

#lastIndexOf'

lastIndexOf' :: Pattern -> Int -> String -> Maybe Int

Returns the index of the last occurrence of the pattern in the given string, starting at the specified index and searching backwards towards the beginning of the string. Returns Nothing if there is no match.

lastIndexOf' (Pattern "a") 1 "ababa" == Just 0
lastIndexOf' (Pattern "a") 3 "ababa" == Just 2
lastIndexOf' (Pattern "a") 4 "ababa" == Just 4

#take

take :: Int -> String -> String

Returns the first n characters of the string.

take 5 "Hello World" == "Hello"

#takeRight

takeRight :: Int -> String -> String

Returns the last n characters of the string.

takeRight 5 "Hello World" == "World"

#takeWhile

takeWhile :: (Char -> Boolean) -> String -> String

Returns the longest prefix (possibly empty) of characters that satisfy the predicate.

takeWhile (_ /= ':') "http://purescript.org" == "http"

#drop

drop :: Int -> String -> String

Returns the string without the first n characters.

drop 6 "Hello World" == "World"

#dropRight

dropRight :: Int -> String -> String

Returns the string without the last n characters.

dropRight 6 "Hello World" == "Hello"

#dropWhile

dropWhile :: (Char -> Boolean) -> String -> String

Returns the suffix remaining after takeWhile.

dropWhile (_ /= '.') "Test.purs" == ".purs"

#slice

slice :: Int -> Int -> String -> Maybe String

Returns the substring at indices [begin, end). If either index is negative, it is normalised to length s - index, where s is the input string. Nothing is returned if either index is out of bounds or if begin > end after normalisation.

slice 0 0   "purescript" == Just ""
slice 0 1   "purescript" == Just "p"
slice 3 6   "purescript" == Just "esc"
slice (-4) (-1) "purescript" == Just "rip"
slice (-4) 3  "purescript" == Nothing

#splitAt

splitAt :: Int -> String -> { after :: String, before :: String }

Splits a string into two substrings, where before contains the characters up to (but not including) the given index, and after contains the rest of the string, from that index on.

splitAt 2 "Hello World" == { before: "He", after: "llo World"}
splitAt 10 "Hi" == { before: "Hi", after: ""}

Thus the length of (splitAt i s).before will equal either i or length s, if that is shorter. (Or if i is negative the length will be 0.)

In code:

length (splitAt i s).before == min (max i 0) (length s)
(splitAt i s).before <> (splitAt i s).after == s
splitAt i s == {before: take i s, after: drop i s}

Modules