From a8f009dc294d55b12f29c32afbdd3b3a43582f75 Mon Sep 17 00:00:00 2001 From: Mathias Jean Johansen Date: Sun, 1 May 2022 13:35:07 +0200 Subject: Add `capitalize` function to `String`s. --- src/gleam/string.gleam | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src') diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam index 3cec7d2..d574242 100644 --- a/src/gleam/string.gleam +++ b/src/gleam/string.gleam @@ -785,3 +785,24 @@ pub fn last(s: String) -> Option(String) { _ -> Some(slice(s, length(s) - 1, 1)) } } + +/// Creates a new `String` with the first grapheme in the input `String` +/// converted to uppercase and the remaining graphemes to lowercase. +/// +/// ## Examples +/// +/// ```gleam +/// > capitalize("mamouna") +/// "Mamouna" +/// ``` +/// +pub fn capitalize(s: String) -> String { + let first = + slice(s, 0, 1) + |> uppercase + let rest = + slice(s, 1, length(s)) + |> lowercase + + append(to: first, suffix: rest) +} -- cgit v1.2.3