Skip to content

Instantly share code, notes, and snippets.

@tautologico
Created April 29, 2011 22:42
Show Gist options
  • Save tautologico/949169 to your computer and use it in GitHub Desktop.
Save tautologico/949169 to your computer and use it in GitHub Desktop.
Split a string into a list of string by breaking "words" separated by spaces
let split s =
let l = String.length s in
let rec whitespace i =
if i >= l then [] else
if s.[i] = ' ' then whitespace (i+1) else nonws i 1
and nonws i j =
if (i+j) = l then [(i,j)] else
if s.[i+j] = ' ' then (i,j) :: whitespace (i+j) else nonws i (j+1) in
List.map (fun (i, l) -> String.sub s i l) (whitespace 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment