Module

Data.String

Package
purescript-strings
Repository
purescript/purescript-strings

Wraps the functions of Javascript's String object. A String represents a sequence of characters. For details of the underlying implementation, see String Reference at MDN.

#Pattern Source

newtype Pattern

A newtype used in cases where there is a string to be matched.

pursPattern = Pattern ".purs"
--can be used like this:
contains pursPattern "Test.purs"
   == true

Constructors

Instances

#Replacement Source

newtype Replacement

A newtype used in cases to specify a replacement for a pattern.

Constructors

Instances

#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

#charCodeAt Source

charCodeAt :: Int -> String -> Maybe Int

Returns the numeric Unicode value of the character at the given index, if the index is within bounds.

charCodeAt 2 "5 €" == Just 0x20AC
charCodeAt 10 "5 €" == Nothing

#fromCharArray Source

fromCharArray :: Array Char -> String

Converts an array of characters into a string.

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

#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

#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

#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

#null Source

null :: String -> Boolean

Returns true if the given string is empty.

null "" == true
null "Hi" == false

#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

#singleton Source

singleton :: Char -> String

Returns a string of length 1 containing the given character.

singleton 'l' == "l"

#localeCompare Source

localeCompare :: String -> String -> Ordering

Compare two strings in a locale-aware fashion. This is in contrast to the Ord instance on String which treats strings as arrays of code units:

"ä" `localeCompare` "b" == LT
"ä" `compare` "b" == GT

#replace Source

replace :: Pattern -> Replacement -> String -> String

Replaces the first occurence of the pattern with the replacement string.

replace (Pattern "<=") (Replacement "≤") "a <= b <= c" == "a ≤ b <= c"

#replaceAll Source

replaceAll :: Pattern -> Replacement -> String -> String

Replaces all occurences of the pattern with the replacement string.

replaceAll (Pattern "<=") (Replacement "≤") "a <= b <= c" == "a ≤ b ≤ c"

#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.

take 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"

#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

#count Source

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

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

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

#split Source

split :: Pattern -> String -> Array String

Returns the substrings of the second string separated along occurences of the first string.

split (Pattern " ") "hello world" == ["hello", "world"]

#splitAt Source

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

Returns the substrings of a split at the given index, if the index is within bounds.

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

#toCharArray Source

toCharArray :: String -> Array Char

Converts the string into an array of characters.

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

#toLower Source

toLower :: String -> String

Returns the argument converted to lowercase.

toLower "hElLo" == "hello"

#toUpper Source

toUpper :: String -> String

Returns the argument converted to uppercase.

toUpper "Hello" == "HELLO"

#trim Source

trim :: String -> String

Removes whitespace from the beginning and end of a string, including whitespace characters and line terminators.

trim "   Hello  \n World\n\t    " == "Hello  \n World"

#joinWith Source

joinWith :: String -> Array String -> String

Joins the strings in the array together, inserting the first argument as separator between them.

joinWith ", " ["apple", "banana", "orange"] == "apple, banana, orange"