Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to split a string by a delimiter functionally?

I tried to write the program in Haskell that will take a string of integer numbers delimitated by comma, convert it to list of integer numbers and increment each number by 1.

For example "1,2,-5,-23,15" -> [2,3,-4,-22,16]

Below is the resulting program

import Data.List  main :: IO () main = do   n <- return 1   putStrLn . show . map (+1) . map toInt . splitByDelimiter delimiter     $ getList n  getList :: Int -> String getList n = foldr (++) [] . intersperse [delimiter] $ replicate n inputStr  delimiter = ','  inputStr = "1,2,-5,-23,15"  splitByDelimiter :: Char -> String -> [String] splitByDelimiter _ "" = [] splitByDelimiter delimiter list =   map (takeWhile (/= delimiter) . tail)     (filter (isPrefixOf [delimiter])        (tails            (delimiter : list)))  toInt :: String -> Int toInt = read 

The most hard part for me was programming of function splitByDelimiter that take a String and return list of Strings

"1,2,-5,-23,15" -> ["1","2","-5","-23","15"]

Thought it is working, I am not happy with the way it is written. There are a lot of parentheses, so it looks Lisp like. Also the algorithm is somewhat artificial:

  1. Prepend delimiter to beginning of string ",1,2,-5,-23,15"

  2. Generate list of all tails [",1,2,-5,-23,15", "1,2,-5,-23,15", ",2,-5,-23,15", .... ]

  3. Filter and left only strings that begins with delimiter [",1,2,-5,-23,15", ",2,-5,-23,15", .... ]

  4. Drop first delimiter and take symbols until next delimiter will be met ["1", "2", .... ]

So the questions are:

How I can improve function splitByDelimiter?

Can I remove prepend and drop of delimiter and make direct split of string?

How I can rewrite the function so there will be less parentheses?

May be I miss something and there are already standard function with this functionality?

like image 414
sign Avatar asked Dec 21 '10 21:12

sign


People also ask

How do you split a string with a delimiter?

You can use the split() method of String class from JDK to split a String based on a delimiter e.g. splitting a comma-separated String on a comma, breaking a pipe-delimited String on a pipe, or splitting a pipe-delimited String on a pipe.

Which string function splits a string based on a delimiter?

Split is used to break a delimited string into substrings. You can use either a character array or a string array to specify zero or more delimiting characters or strings. If no delimiting characters are specified, the string is split at white-space characters.

What is the best way to split a string in Python?

The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

Which function is used to split the string?

Splitting a string using strtok() in C In C, the strtok() function is used to split a string into a series of tokens based on a particular delimiter.


1 Answers

Doesn't Data.List.Split.splitOn do this?

like image 98
Mikel Avatar answered Sep 25 '22 19:09

Mikel