Skip to content

Commit 9059365

Browse files
authored
Merge pull request #100 from purescript/nonempty
Non-empty strings & various operations
2 parents 81cfa1a + 48c7756 commit 9059365

File tree

5 files changed

+789
-5
lines changed

5 files changed

+789
-5
lines changed

src/Data/String.purs

+22-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ module Data.String
2121
, replace
2222
, replaceAll
2323
, take
24+
, takeRight
2425
, takeWhile
2526
, drop
27+
, dropRight
2628
, dropWhile
2729
, stripPrefix
2830
, stripSuffix
@@ -274,8 +276,8 @@ foreign import _lastIndexOf
274276
-> Maybe Int
275277

276278
-- | Returns the index of the last occurrence of the pattern in the
277-
-- | given string, starting at the specified index
278-
-- | and searching backwards towards the beginning of the string.
279+
-- | given string, starting at the specified index
280+
-- | and searching backwards towards the beginning of the string.
279281
-- | Returns `Nothing` if there is no match.
280282
-- |
281283
-- | ```purescript
@@ -347,6 +349,15 @@ foreign import replaceAll :: Pattern -> Replacement -> String -> String
347349
-- |
348350
foreign import take :: Int -> String -> String
349351

352+
-- | Returns the last `n` characters of the string.
353+
-- |
354+
-- | ```purescript
355+
-- | take 5 "Hello World" == "World"
356+
-- | ```
357+
-- |
358+
takeRight :: Int -> String -> String
359+
takeRight i s = drop (length s - i) s
360+
350361
-- | Returns the string without the first `n` characters.
351362
-- |
352363
-- | ```purescript
@@ -355,6 +366,15 @@ foreign import take :: Int -> String -> String
355366
-- |
356367
foreign import drop :: Int -> String -> String
357368

369+
-- | Returns the string without the last `n` characters.
370+
-- |
371+
-- | ```purescript
372+
-- | dropRight 6 "Hello World" == "Hello"
373+
-- | ```
374+
-- |
375+
dropRight :: Int -> String -> String
376+
dropRight i s = take (length s - i) s
377+
358378
-- | Returns the number of contiguous characters at the beginning
359379
-- | of the string for which the predicate holds.
360380
-- |

0 commit comments

Comments
 (0)