Module

Data.String.CodeUnits

Package
purescript-strings
Repository
purescript/purescript-strings

#stripPrefix Source

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 Source

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 Source

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 Source

singleton :: Char -> String

Returns a string of length 1 containing the given character.

singleton 'l' == "l"

#fromCharArray Source

fromCharArray :: Array Char -> String

Converts an array of characters into a string.

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

#toCharArray Source

toCharArray :: String -> Array Char

Converts the string into an array of characters.

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

#charAt Source

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 Source

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 Source

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 Source

length :: String -> Int

Returns the number of characters the string is composed of.

length "Hello World" == 11

#countPrefix Source

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 Source

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' Source

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 Source

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' Source

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 Source

take :: Int -> String -> String

Returns the first n characters of the string.

take 5 "Hello World" == "Hello"

#takeRight Source

takeRight :: Int -> String -> String

Returns the last n characters of the string.

takeRight 5 "Hello World" == "World"

#takeWhile Source

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

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

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

#drop Source

drop :: Int -> String -> String

Returns the string without the first n characters.

drop 6 "Hello World" == "World"

#dropRight Source

dropRight :: Int -> String -> String

Returns the string without the last n characters.

dropRight 6 "Hello World" == "Hello"

#dropWhile Source

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

Returns the suffix remaining after takeWhile.

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

#slice Source

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 Source

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}