Capitalizing a string

Elm:
Capitalizing a string

How to:

In Elm, there isn’t a built-in function specifically for capitalizing strings. However, you can achieve this easily by using the built-in String module functions like toUpper, toLower, left, and dropLeft.

capitalize : String -> String
capitalize str =
    if String.isEmpty str then
        ""
    else
        String.toUpper (String.left 1 str) ++ String.toLower (String.dropLeft 1 str)

-- Example usage
main =
    String.toList "hello world" |> List.map capitalize |> String.join " "
    -- Output: "Hello World"

For more complex scenarios or if you prefer using a library that provides a direct way to capitalize strings, you might consider a third-party package such as elm-community/string-extra. However, as of my last update, Elm’s ecosystem encourages dealing with such tasks using built-in functions to keep the language and projects lean.

import String.Extra as StringExtra

-- In case there's a `capitalize` function in a third-party library
capitalizeWithLibrary : String -> String
capitalizeWithLibrary str =
    StringExtra.capitalize str

-- Example usage with hypothetical library function
main =
    "this is elm" |> capitalizeWithLibrary
    -- Hypothetical output: "This is elm"

Always check the Elm package repository for the latest and most preferred libraries for string manipulation if you’re looking for additional functionality beyond the standard library.