Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions docs/Data/String.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`

Expand Down
13 changes: 13 additions & 0 deletions src/Data/String.purs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ module Data.String
, drop
, dropWhile
, stripPrefix
, stripSuffix
, split
, toCharArray
, toLower
Expand Down Expand Up @@ -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

Expand Down