[erlang-questions] string title bif?

Wes James comptekki@REDACTED
Fri Jun 25 20:53:52 CEST 2010


title("this is a test") returns "This is a test".  It only uppercases
the first word.  This could work to replace my fc_toupper (first char
to upper), though.

thx,

-wes

On Fri, Jun 25, 2010 at 12:40 PM, Zoltan Lajos Kis <kiszl@REDACTED> wrote:
> I think a tiny state-machine with "mapfoldl" is a cleaner (albeit more
> imperative :o)) approach for this.
>
> title(String) ->
>    {Title, _} = lists:mapfoldl(fun f/2, 'sep', String),
>    Title.
>
> f($_, _) -> {$ , 'sep'};
> f(C, 'sep') -> {string:to_upper(C), 'char'};
> f(C, _) -> {C, 'char'}.
>
> Regards,
> Zoltan.
>
>
> On 6/25/2010 8:23 PM, Wes James wrote:
>>
>> Is there a BIF that does this (I can't find one, if there is):
>>
>> string:title("this_is_a_title") ->  "This Is A Title".
>>
>> Or something like this?  If not I'll just create one.
>>
>> It would need to split the string at the "_" and then uppercase the
>> first character of each word.
>>
>> This test works:
>>
>> -------------------
>>
>> -module(title).
>>
>> -export([start/0]).
>>
>> start() ->
>>        title(string:tokens("this_is_a_title","_")).
>>
>> title([H|T]) ->
>>        [fc_upper(H)|title(T)];
>> title([]) ->  [].
>>
>> fc_upper([C|R]) ->
>>        [string:to_upper(C)]++R.
>>
>> ----------------
>>
>> thx.
>>
>> -wes
>>
>> ________________________________________________________________
>> erlang-questions (at) erlang.org mailing list.
>> See http://www.erlang.org/faq.html
>> To unsubscribe; mailto:erlang-questions-unsubscribe@REDACTED
>>
>>
>
>


More information about the erlang-questions mailing list