如何在Erlang中将字符串的第一个字母转换为大写

可以使用名为 string:titlecase/1 的函数,如下所示:

Eshell V11.0  (abort with ^G)
1> string:titlecase("hello").
"Hello"
2>

或者...如果您不想将整个字符串首字母大写,而只想将第一个单词首字母大写...

2> [First|Rest] = string:lexemes("this name is Nice", [$\s]).
["this","name","is","Nice"]
3> string:join([string:titlecase(First)|Rest], " ").
"This name is Nice"
4>

但是如果你不想使用花哨的字符串函数,你可以使用模式匹配...

4> f(Rest).
ok
5> [FirstChar|Rest] = "hello".
"hello"
6> [string:to_upper(FirstChar)|Rest].
"Hello"
7>

猜你喜欢

转载自blog.csdn.net/qq_25231683/article/details/131259643