Skip to main content
ubuntuask.com

Back to all posts

How to Convert the First Letter Of A String to Uppercase In Erlang?

Published on
3 min read
How to Convert the First Letter Of A String to Uppercase In Erlang? image

Best Erlang Programming Guides to Buy in October 2025

1 Programming Erlang: Software for a Concurrent World (Pragmatic Programmers)

Programming Erlang: Software for a Concurrent World (Pragmatic Programmers)

BUY & SAVE
$27.34 $42.00
Save 35%
Programming Erlang: Software for a Concurrent World (Pragmatic Programmers)
2 Erlang Programming: A Concurrent Approach to Software Development

Erlang Programming: A Concurrent Approach to Software Development

BUY & SAVE
$39.99
Erlang Programming: A Concurrent Approach to Software Development
3 The BEAM Book: Understanding the Erlang Runtime System

The BEAM Book: Understanding the Erlang Runtime System

BUY & SAVE
$25.00
The BEAM Book: Understanding the Erlang Runtime System
4 Learn You Some Erlang for Great Good!: A Beginner's Guide

Learn You Some Erlang for Great Good!: A Beginner's Guide

  • AFFORDABLE PRICES ON QUALITY USED BOOKS FOR BUDGET-CONSCIOUS READERS.
  • ECO-FRIENDLY OPTION PROMOTING SUSTAINABILITY THROUGH BOOK REUSE.
  • DETAILED DESCRIPTIONS AND PHOTOS ENSURE BUYER CONFIDENCE AND TRUST.
BUY & SAVE
$59.99
Learn You Some Erlang for Great Good!: A Beginner's Guide
5 Designing for Scalability with Erlang/OTP: Implement Robust, Fault-Tolerant Systems

Designing for Scalability with Erlang/OTP: Implement Robust, Fault-Tolerant Systems

BUY & SAVE
$40.08 $49.99
Save 20%
Designing for Scalability with Erlang/OTP: Implement Robust, Fault-Tolerant Systems
6 Programming Erlang: Software for a Concurrent World

Programming Erlang: Software for a Concurrent World

BUY & SAVE
$31.50 $36.95
Save 15%
Programming Erlang: Software for a Concurrent World
7 Introducing Erlang: Getting Started in Functional Programming

Introducing Erlang: Getting Started in Functional Programming

BUY & SAVE
$23.10 $39.99
Save 42%
Introducing Erlang: Getting Started in Functional Programming
+
ONE MORE?

To convert the first letter of a string to uppercase in Erlang, you can follow these steps:

  1. Extract the first character of the string using the hd/1 function. For example, if your string is stored in the variable Str, you can extract the first character using FirstChar = hd(Str).
  2. Convert the first character to uppercase using the lists:uppercase/1 function. This function takes a string as input and returns the same string with all characters converted to uppercase. In this case, you only need to convert the first character, so you can use UppercaseFirstChar = lists:uppercase([FirstChar]).
  3. Now, you have the first character in uppercase. To get the rest of the string (excluding the first character), you can use the tl/1 function. For example, RestOfString = tl(Str).
  4. Concatenate the uppercase first character with the rest of the string. You can use the ++/2 operator for string concatenation. For instance, Result = UppercaseFirstChar ++ RestOfString.

By following these steps, you can convert the first letter of a string to uppercase in Erlang.

What is the function in Erlang for title-case formatting of strings?

There is no built-in function in Erlang for title-case formatting of strings. However, you can create a custom function to achieve this. Here is an example implementation:

-module(string_utils). -export([title_case/1]).

title_case(String) -> Words = string:tokens(String, " "), CapitalizedWords = [capitalize_word(Word) || Word <- Words], string:join(CapitalizedWords, " ").

capitalize_word([FirstChar | Rest]) -> [uppercase(FirstChar) | Rest].

To use this function, you can compile the module and call string_utils:title_case/1 with a string as an argument:

1> c(string_utils). {ok,string_utils} 2> string_utils:title_case("hello world"). "Hello World" 3> string_utils:title_case("welcome to erlang"). "Welcome To Erlang"

The title_case/1 function splits the input string into words, capitalizes the first character of each word using capitalize_word/1, and then joins the words back into a single string. The capitalize_word/1 function takes a list representing a word and returns a new list where the first character is capitalized.

What is the syntax for converting the first letter of a string to uppercase in Erlang?

To convert the first letter of a string to uppercase in Erlang, you can use the string:capitalize/1 function. The syntax is as follows:

string:capitalize(String)

Here, String is the input string that you want to capitalize the first letter of. The function returns a new string with the first letter capitalized.

Here's an example:

1> string:capitalize("hello"). "Hello" 2> string:capitalize("world"). "World"

In the above example, the first letter of the input strings "hello" and "world" is capitalized using the string:capitalize/1 function.

How to ensure the first letter of a string is always capitalized in Erlang?

To ensure that the first letter of a string is always capitalized in Erlang, you can use the string:to_titlecase/1 function along with the hd/1 and tl/1 functions. Here's an example of how you can achieve this:

capitalize_first_letter(String) -> [First | Rest] = String, CapitalizedFirst = string:to_titlecase([First]), CapitalizedFirst ++ Rest.

This capitalize_first_letter/1 function takes a string as input and then splits it into the first character (First) and the remaining characters (Rest). It then uses string:to_titlecase/1 to capitalize the first character and finally concatenates the capitalized first character with the remaining characters.

Here's an example usage:

1> MyString = "hello world". "hello world" 2> capitalize_first_letter(MyString). "Hello world"

In this example, the input string "hello world" is transformed into "Hello world" with the first letter capitalized.