Skip to content

Commit 65f973a

Browse files
committed
Merge pull request #37 from hdgarrood/stripPrefix
Add stripPrefix
2 parents 1eca8a1 + 693f1a2 commit 65f973a

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

docs/Data/String.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ dropWhile :: (Char -> Boolean) -> String -> String
7878

7979
Returns the suffix remaining after `takeWhile`.
8080

81+
#### `stripPrefix`
82+
83+
``` purescript
84+
stripPrefix :: String -> String -> Maybe String
85+
```
86+
87+
If the string starts with the given prefix, return the portion of the
88+
string left after removing it, as a Just value. Otherwise, return Nothing.
89+
* `stripPrefix "http:" "http://purescript.org" == Just "//purescript.org"
90+
* `stripPrefix "http:" "https://purescript.org" == Nothing
91+
8192
#### `fromCharArray`
8293

8394
``` purescript

src/Data/String.purs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module Data.String
2323
, takeWhile
2424
, drop
2525
, dropWhile
26+
, stripPrefix
2627
, split
2728
, toCharArray
2829
, toLower
@@ -94,6 +95,16 @@ takeWhile p s = take (count p s) s
9495
dropWhile :: (Char -> Boolean) -> String -> String
9596
dropWhile p s = drop (count p s) s
9697

98+
-- | If the string starts with the given prefix, return the portion of the
99+
-- | string left after removing it, as a Just value. Otherwise, return Nothing.
100+
-- | * `stripPrefix "http:" "http://purescript.org" == Just "//purescript.org"
101+
-- | * `stripPrefix "http:" "https://purescript.org" == Nothing
102+
stripPrefix :: String -> String -> Maybe String
103+
stripPrefix prefix str =
104+
case indexOf prefix str of
105+
Just 0 -> Just $ drop (length prefix) str
106+
_ -> Nothing
107+
97108
-- | Converts an array of characters into a string.
98109
foreign import fromCharArray :: Array Char -> String
99110

0 commit comments

Comments
 (0)