Module

Data.String.CodePoints

Package
purescript-strings
Repository
purescript/purescript-strings

These functions allow PureScript strings to be treated as if they were sequences of Unicode code points instead of their true underlying implementation (sequences of UTF-16 code units). For nearly all uses of strings, these functions should be preferred over the ones in Data.String.

#CodePoint Source

newtype CodePoint

CodePoint is an Int bounded between 0 and 0x10FFFF, corresponding to Unicode code points.

Instances

#codePointToInt Source

#codePointAt Source

codePointAt :: Int -> String -> Maybe CodePoint

Returns the first code point of the string after dropping the given number of code points from the beginning, if there is such a code point. Operates in constant space and in time linear to the given index.

#count Source

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

Returns the number of code points in the leading sequence of code points which all match the given predicate. Operates in constant space and in time linear to the length of the string.

#drop Source

drop :: Int -> String -> String

Drops the given number of code points from the beginning of the string. If the string does not have that many code points, returns the empty string. Operates in constant space and in time linear to the given number.

#dropWhile Source

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

Drops the leading sequence of code points which all match the given predicate from the string. Operates in constant space and in time linear to the length of the string.

#fromCodePointArray Source

fromCodePointArray :: Array CodePoint -> String

Creates a string from an array of code points. Operates in space and time linear to the length of the array.

#indexOf Source

indexOf :: Pattern -> String -> Maybe Int

Returns the number of code points preceding the first match of the given pattern in the string. Returns Nothing when no matches are found.

#indexOf' Source

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

Returns the number of code points preceding the first match of the given pattern in the string. Pattern matches preceding the given index will be ignored. Returns Nothing when no matches are found.

#lastIndexOf Source

lastIndexOf :: Pattern -> String -> Maybe Int

Returns the number of code points preceding the last match of the given pattern in the string. Returns Nothing when no matches are found.

#lastIndexOf' Source

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

Returns the number of code points preceding the first match of the given pattern in the string. Pattern matches following the given index will be ignored. Returns Nothing when no matches are found.

#length Source

length :: String -> Int

Returns the number of code points in the string. Operates in constant space and in time linear to the length of the string.

#singleton Source

singleton :: CodePoint -> String

Creates a string containing just the given code point. Operates in constant space and time.

#splitAt Source

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

Returns a record with strings created from the code points on either side of the given index. If the index is not within the string, Nothing is returned.

#take Source

take :: Int -> String -> String

Returns a string containing the given number of code points from the beginning of the given string. If the string does not have that many code points, returns the empty string. Operates in constant space and in time linear to the given number.

#takeWhile Source

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

Returns a string containing the leading sequence of code points which all match the given predicate from the string. Operates in constant space and in time linear to the length of the string.

#toCodePointArray Source

toCodePointArray :: String -> Array CodePoint

Creates an array of code points from a string. Operates in space and time linear to the length of the string.

#uncons Source

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

Returns a record with the first code point and the remaining code points of the string. Returns Nothing if the string is empty. Operates in constant space and time.

Re-exports from Data.String

#Replacement Source

newtype Replacement

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

Constructors

Instances

#Pattern Source

newtype Pattern

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

Constructors

Instances

#trim Source

trim :: String -> String

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

#toUpper Source

toUpper :: String -> String

Returns the argument converted to uppercase.

#toLower Source

toLower :: String -> String

Returns the argument converted to lowercase.

#toCharArray Source

toCharArray :: String -> Array Char

Converts the string into an array of characters.

#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

#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

#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"]

#replaceAll Source

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

Replaces all occurences of the first argument with the second argument.

#replace Source

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

Replaces the first occurence of the first argument with the second argument.

#null Source

null :: String -> Boolean

Returns true if the given string is empty.

#localeCompare Source

localeCompare :: String -> String -> Ordering

Locale-aware sort order comparison.

#joinWith Source

joinWith :: String -> Array String -> String

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

#fromCharArray Source

fromCharArray :: Array Char -> String

Converts an array of characters into a string.

#contains Source

contains :: Pattern -> String -> Boolean

Checks whether the first string exists in the second string.

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

#charAt Source

charAt :: Int -> String -> Maybe Char

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