fp | FP‎ > ‎fp | F#‎ > ‎f# | code sample‎ > ‎

f#cs | active pattern

http://stackoverflow.com/questions/3722591/pattern-matching-on-the-beginning-of-a-string-in-f

let (|Prefix|_|) (p:string) (s:string) =
    if s.StartsWith(p) then
        Some(s.Substring(p.Length))
    else
        None

match "Hello world" with
| Prefix "The" rest -> printfn "Started with 'The', rest is %s" rest
| Prefix "Hello" rest -> printfn "Started with 'Hello', rest is %s" rest
| _ -> printfn "neither"



Comments