如何将字符串分成子串?

时间:2016-10-25 21:51:50

标签: erlang

我想根据给定的数字将字符串分成子字符串,例如:

divide("string",1) = ["s","t","r","i","n","g"].

我试过这个,但没有成功。

lists:split(1,"string") = {"s", "tring"}

有什么想法吗?

4 个答案:

答案 0 :(得分:4)

我会计算一次长度(因为它是一个缓慢的操作),然后递归使用lists:split/2,直到剩下的列表小于N

divide(List, N) ->
  divide(List, N, length(List)).

divide(List, N, Length) when Length > N ->
  {A, B} = lists:split(N, List),
  [A | divide(B, N, Length - N)];
divide(List, _, _) ->
  [List].
1> c(a).
{ok,a}
2> a:divide("string", 1).
["s","t","r","i","n","g"]
3> a:divide("string", 2).
["st","ri","ng"]
4> a:divide("string", 3).
["str","ing"]
5> a:divide("string", 4).
["stri","ng"]
6> a:divide("string", 5).
["strin","g"]
7> a:divide("string", 6).
["string"]
8> a:divide("string", 7).
["string"]

答案 1 :(得分:2)

我认为@Dogbert解决方案目前是最好的...但这里是一个带递归循环的其他实现示例。

divide_test() ->
    [?assertEqual(divide("string",1), ["s","t","r","i","n","g"]),
     ?assertEqual(divide("string",2), ["st","ri","ng"]),
     ?assertEqual(divide("string",3), ["str","ing"]),
     ?assertEqual(divide("string",4), ["stri","ng"])
    ].

-spec divide(list(), integer()) -> list(list()).
divide(String, Size) 
  when is_list(String), is_integer(Size) ->
    divide(String, Size, 0, [], []).

-spec divide(list(), integer(), integer(), list(), list()) -> list(list()).
divide([], _, _, Buf, Result) ->
    Return = [lists:reverse(Buf)] ++ Result,
    lists:reverse(Return);
divide([H|T], Size, 0, Buf, Result) ->    
    divide(T, Size, 1, [H] ++ Buf, Result);
divide([H|T], Size, Counter, Buf, Result) ->    
    case Counter rem Size =:= 0 of
    true ->
        divide(T, Size, Counter+1, [H] ++ [], [lists:reverse(Buf)] ++ Result);
    false ->
        divide(T, Size, Counter+1, [H] ++ Buf, Result)
    end.

答案 2 :(得分:1)

您可以尝试此功能。如果数字是> 0小于或等于字符串长度除以2。

first_substring(List, Separator) ->
     first_substring_loop(List, Separator, []).

first_substring_loop([], _, Reversed_First) ->
     lists:reverse(Reversed_First);
first_substring_loop(List, Separator, Reversed_First) ->
[H|T]= my_tuple_to_list(lists:split(Separator,List)),
       first_substring_loop(lists:flatten(T), Separator, [H|Reversed_First]).

my_tuple_to_list(Tuple) -> [element(T, Tuple) || T <- lists:seq(1, tuple_size(Tuple))].

结果是

1> fact:first_substring("string", 1).
["s","t","r","i","n","g"]
2> fact:first_substring("string", 2).
["st","ri","ng"]
3> fact:first_substring("string", 3).
["str","ing"]

答案 3 :(得分:1)

一个简单的简单解决方案可以是:

divide(String, Length) -> divide(String, Length, []).
divide([], _, Acc) -> Acc;
divide(String, Length, Acc) ->
    {Res, Rest} = lists:split(min(Length, length(String)), String),
    divide(Rest, Length, Acc ++ [Res]).

对于长度为1的特定分割情况,可以使用列表推导:

ListOfLetters = [[Letter] || Letter <- String].