Home
Insert
Update snippet 'Chunk String by Size into List of String Chunks'
Title
Passcode
Description
Easy way to chunk string into equal segments. Returns a list of string chunks (as opposed to an array or seq of chunks).
Source code
let chunkStr size str = let rec loop (s:string) accum = let branch = size < s.Length match branch with | true -> loop (s.[size..]) (s.[0..size-1]::accum) | false -> s::accum (loop str []) |> List.rev //example "A man, a plan, a canal: Panama." |> chunkStr 3 //result: ["A m"; "an,"; " a "; "pla"; "n, "; "a c"; "ana"; "l: "; "Pan"; "ama"; "."]
Tags
list
string
list
string
Author
Link
Reference NuGet packages
If your snippet has external dependencies, enter the names of NuGet packages to reference, separated by a comma (
#r
directives are not required).
Update