How to convert first Letter of string to uppercase in erlang

232 Views Asked by At

I have a string "data".How can I convert only the first character to uppercase and get a new string in the form of "Data"?

2

There are 2 best solutions below

0
Brujo Benavides On

You can use the amazing function called string:titlecase/1, like this:

1> string:titlecase("data").
"Data"

Or… if you don't want to title-case your whole string, but just the first word…

5> [First|Rest] = string:lexemes("this data is Nice", [$\s]).
["this","data","is","Nice"]
6> string:join([string:titlecase(First)|Rest], " ").
"This data is Nice"

But if you want no fancy string function, you can just use pattern-matching…

11> [FirstChar|Rest] = "data".
"data"
12> [string:to_upper(FirstChar)|Rest].
"Data"
13>
0
Akshit Verma On

Use the titlecase function in string module.

string:titlecase("data").