diff --git a/docs/Data/String.md b/docs/Data/String.md index b5f4771..77397fb 100644 --- a/docs/Data/String.md +++ b/docs/Data/String.md @@ -89,6 +89,17 @@ string left after removing it, as a Just value. Otherwise, return Nothing. * `stripPrefix "http:" "http://purescript.org" == Just "//purescript.org"` * `stripPrefix "http:" "https://purescript.org" == Nothing` +#### `stripSuffix` + +``` purescript +stripSuffix :: String -> 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 ".exe" "psc.exe" == Just "psc"` +* `stripSuffix ".exe" "psc" == Nothing` + #### `fromCharArray` ``` purescript @@ -198,8 +209,9 @@ of the string for which the predicate holds. split :: String -> String -> Array String ``` -Returns the substrings of the first string separated along occurences -of the second string. +Returns the substrings of the second string separated along occurences +of the first string. +* `split " " "hello world" == ["hello", "world"]` #### `toCharArray` diff --git a/src/Data/String.purs b/src/Data/String.purs index 652d077..0b104d7 100644 --- a/src/Data/String.purs +++ b/src/Data/String.purs @@ -24,6 +24,7 @@ module Data.String , drop , dropWhile , stripPrefix + , stripSuffix , split , toCharArray , toLower @@ -105,6 +106,18 @@ stripPrefix prefix str = Just 0 -> Just $ drop (length prefix) str _ -> Nothing +-- | 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 ".exe" "psc.exe" == Just "psc"` +-- | * `stripSuffix ".exe" "psc" == Nothing` +stripSuffix :: String -> String -> Maybe String +stripSuffix suffix str = + case lastIndexOf suffix str of + Just x | x == length str - length suffix -> + Just $ take x str + _ -> + Nothing + -- | Converts an array of characters into a string. foreign import fromCharArray :: Array Char -> String