Erlang/OTP Forums

Author Message

<  Erlang  ~  splitting a string into 2 substring

l1bby
Posted: Wed Sep 10, 2008 12:12 am Reply with quote
Joined: 09 Sep 2008 Posts: 1
Hi All,

I am new to erlang and I want to write Erlang function that given a string and a pattern and uses the pattern to split the string into like this:

For example if the pattern is "ab" and the string is "acabcabts" the code will return ["ac", "cabts"].

If the pattern is "bc" and the string is "bcad" the code returns ["", ad].

If the pattern is "ac" and the string is "bcad" the code will return ["bcad", ""].

I'm trying to make function using reverse list but still stuck there. Can anybody help me? Thank's so much.
View user's profile Send private message
dsmith
Posted: Wed Sep 10, 2008 10:43 pm Reply with quote
User Joined: 08 Aug 2007 Posts: 41 Location: Toronto
It may not be exactly what you are looking for but you could try the regexp module.


Code:

19> regexp:split("acabcabts", "ab"). 
{ok,["ac","c","ts"]}
20> regexp:split("bcad", "ad").     
{ok,["bc",[]]}
21> regexp:split("bcad", "ac").
{ok,["bcad"]}
View user's profile Send private message
dsmith
Posted: Thu Sep 11, 2008 12:57 am Reply with quote
User Joined: 08 Aug 2007 Posts: 41 Location: Toronto
I wrote a little module that should do what you need. There are two exports split/2 and split_first/2.

It should a least get you started.

Code:

-module(string_misc).
-export([split_first/2,split/2]).

split(String, Delim) ->
    split(String ,Delim, []).

split([], _, ListAcc) ->
   lists:reverse(ListAcc);
split(String, Delim, ListAcc) ->
   [FirstPart, SecondPart] = split_first(String, Delim),
   split(SecondPart, Delim, [FirstPart|ListAcc]).

split_first(String, Delim) ->
    split_first(String, Delim, []).
   
split_first([], _, FirstPart) ->
   [lists:reverse(FirstPart), []];
split_first([H|T], [H|DT]=Delim, FirstPart) ->
   case split_match(T, DT) of
      no_match ->
         split_first(T, Delim, [H|FirstPart]);
      Rest ->
         [lists:reverse(FirstPart), Rest]
   end;
split_first([H|T], Delim, PartAcc) ->
   split_first(T, Delim, [H|PartAcc]).

split_match(String, []) ->
   String;
split_match([H|T], [H|DT]) ->
   split_match(T, DT);
split_match(_, _) ->
    no_match.


Code:

177> string_misc:split_first("acabcabts","ab").         
["ac","cabts"]
178> string_misc:split_first("bcad","bc").     
[[],"ad"]
179> string_misc:split_first("bcad","ac").     
["bcad",[]]
180> string_misc:split("acabcabts","ab").     
["ac","c","ts"]
181> string_misc:split("bcad","bc").     
[[],"ad"]
182> string_misc:split("bcad","ac").     
["bcad"]
View user's profile Send private message
wuji
Posted: Mon Aug 13, 2012 6:55 am Reply with quote
User Joined: 10 Aug 2012 Posts: 654
within the Zetas at the time of the cartel's creation creation [h4]jordan 6[/h4] creation several years ago, are now allegedly top leaders of
organization that controls drug trafficking in the east and south south [h4]cheap jordans[/h4] south of Mexico. Miguel, or "40," allegedly runs the Zetas
with "3," Heriberto Lazcano.The Zetas began in 1999 when former former jordan 6 former members of the Mexican military signed on to work
security for the Gulf drug cartel. The Zetas went into into knockoff designer *beep* into business for themselves and are now at war with
Gulf Cartel. The Zetas are based in Nuevo Laredo, in in knockoff designer *beep* in Tamaulipas state just across the border from Laredo, Texas.Click
for the Blotter Homepage. Nashville Restaurant Owner Died Of Of replica designer *beep* Of Carbon Dioxide Poisoning In Cooler, Likely Overcome Within A
M
View user's profile Send private message
dongdongwu
Posted: Thu Sep 20, 2012 5:33 am Reply with quote
User Joined: 19 Sep 2012 Posts: 236
His good friend Diane said: "Christian Louboutin Men Shoes was a magician, he make shoes is immediately put his female people with legs and advantage. He understands women wanted to do and can make them into beautiful Cinderella." Madonna often in its concert wearing Christian louboutin high heels , and some famous superstar like Angelina jolie, mariah Carey, beyonce Knowles, the famous Japanese singer YaYouMei Hamasaki helps Christian Louboutin Men Shoes set up its powerful position. The youngest customers will count Tom cruise's daughter sully cruz. Louboutin made for only a pair of handmade Christian Louboutin high heel Shoes! Want to be more fashion? Put on Christian Louboutin Outlet !
Candy colors of the chalaza high-heeled shoes with lolita type allure, set full finely gem blue "neon shoes" is to need to use "sexy" to describe. Each pair are worth careful appreciation of lithe and graceful fairy ludaoli, what kind of most let you move?Christian Louboutin Men Shoes that one brush red is always cannot resist the temptation, Christian Louboutin men outlet continue to use the days of high 8cm above slender heel proclaim the sexy and luxuriant. The bowknot on black pointed high-heeled shoes with sharp rivet concomitant, wild python met enchanting color printing grain, It is that pairs of high-heeled shoes lets Carrie more feminine flavour. Like Christian Louboutin for men her word.
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum