Skip to content

Instantly share code, notes, and snippets.

@ImkeF
Last active November 30, 2017 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ImkeF/3ba0db54a49c1cb5231ed4b2e9996b89 to your computer and use it in GitHub Desktop.
Save ImkeF/3ba0db54a49c1cb5231ed4b2e9996b89 to your computer and use it in GitHub Desktop.
// Removes all duplicate separators within a string. If optional "TrimEnds" is set to "yes" the (single remaining) separators will be removed from the start and end as well.
(MyText as text, Separator as text, optional TrimEnds as text) as text =>
let
TransformTextToList = List.Buffer(Text.ToList(MyText)),
Result = List.Accumulate(TransformTextToList, // List that will be iterated through
"", // a starting value (if necessary, here we leave it blank)
(resultSoFar, current) => if Text.End(resultSoFar,1) = Separator and current = Separator then resultSoFar else resultSoFar&current // operation that will be performed at each iteration-step
),
TrimEnd = if TrimEnds = "yes" then Text.TrimStart(Text.TrimEnd(Result, Separator), Separator) else Result
in
TrimEnd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment