Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using System.String.Split from F#

Tags:

f#

f#-3.0

I would like to use the .NET CLR version of String.Split in F#. Specifically I would like to use this code:

let main argv = 
    let s = "Now is the time for FOO good men to come to the aide of their country"
    let sepAry = [|"FOO"; "BAR"|]
    let z1 = s.Split sepAry
    0 // return an integer exit code

This fails to compile however, due to the fact (I believe) that the version of Split in F# is implemented differently than the one in .Net 4.5. The version from .NET that I would like is:

Split(String[], StringSplitOptions) Returns a string array that contains the substrings in this string that are delimited by elements of a specified string array. A parameter specifies whether to return empty array elements.

I understand that I am getting the F# version of Split, which formerly resided in the PowerPack and that is why the implementation differs from the CLR version.

What is the best way to get what I want? Is it possible to override the F# version of Split and use the .Net version? Is it possible to extend the F# version and if so, how?

like image 827
JonnyBoats Avatar asked May 27 '13 20:05

JonnyBoats


1 Answers

The overload you want to use expects a second argument.

let z1 = s.Split (sepAry, System.StringSplitOptions.None)

It's not an “F# version of Split”, it's exactly that Split you see in C#.

like image 198
kirelagin Avatar answered Oct 08 '22 09:10

kirelagin