Split string while saving Haskell delimiters

I basically try to split Stringinto [[String]]and then concatform the results, but keeping the delimiters in the resulting list (even repeating in a line).

Something like work below, but the separator gets a crunch in one space instead of keeping all three spaces

unwords . map (\x -> "|" ++ x ++"|") . words $ "foo   bar"
-- "|foo| |bar|"

Ideally, I could get something like:

"|foo||   ||bar|" -- or
"|foo|   |bar|"

I just can’t understand how to save the separator, all the separation functions that I saw dropped the separators from the resulting lists, I can write them myself, but it looks like it will be in the standard library and at this moment I want to learn more than the basics that include getting to know more conversational ways of doing things.

I think I'm looking for some kind of function, for example:

splitWithDelim :: Char -> String -> [String]
splitWithDelim "foo   bar" -- ["foo", " ", " ", " ", "bar"]

, , ?

+4
1

, , keepDelimsL keepDelimsR Data.List.Split, :

split (keepDelimsL $ oneOf "xyz") "aazbxyzcxd" == ["aa","zb","x","y","zc","xd"]
+5

Source: https://habr.com/ru/post/1598940/


All Articles