aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/gleam/string.gleam23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam
index ac7d3aa..3cec7d2 100644
--- a/src/gleam/string.gleam
+++ b/src/gleam/string.gleam
@@ -762,3 +762,26 @@ pub fn first(s: String) -> Option(String) {
_ -> Some(slice(s, 0, 1))
}
}
+
+/// Returns the last element in a grapheme and wraps it in an `Option(String)`.
+/// If the `String` is empty, it returns `None`. Otherwise, it returns
+/// `Some(String)`.
+///
+/// ## Examples
+///
+/// ```gleam
+/// > last("")
+/// None
+/// ```
+///
+/// ```gleam
+/// > last("icecream")
+/// Some("m")
+/// ```
+///
+pub fn last(s: String) -> Option(String) {
+ case length(s) {
+ 0 -> None
+ _ -> Some(slice(s, length(s) - 1, 1))
+ }
+}