[erlang-questions] string title bif?

Hynek Vychodil <>
Mon Jul 12 10:15:29 CEST 2010


You can found that straightforward solution is clearer and faster
(especially with HiPE) for this sort of state machines:

title([C|T]) -> [string:to_upper(C)|title_inner(T)];
title([]) -> [].

title_inner([$_|T]) -> [$\s|title(T)];
title_inner([C|T]) -> [C|title_inner(T)];
title_inner([]) -> [].

On Fri, Jun 25, 2010 at 8:40 PM, Zoltan Lajos Kis <> 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 (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:
>
>



-- 
--Hynek (Pichi) Vychodil

Analyze your data in minutes. Share your insights instantly. Thrill
your boss.  Be a data hero!
Try GoodData now for free: www.gooddata.com


More information about the erlang-questions mailing list