F#, Split String and .Net methods
Asked Answered
P

4

51

I'm new to F#. I'm using VS2008 shell and F# interactive. I try to split a string using "System.String.Split" but then I get the error: "Split is not a static method"

code example:

let Count text =
    let words = System.String.Split [' '] text
    let nWords = words.Length
    (nWords)

How do I use the String methods like split in F# ?

Padauk answered 2/3, 2010 at 22:10 Comment(0)
L
61

You call them as instance methods:

let Count (text : string) =
  let words = text.Split [|' '|]
  let nWords = words.Length
  (nWords)

(Note you need to use [| |] because Split takes an array not a list; or, as per Joel Mueller's comment, because Split takes a params array, you can just pass in the delimiters as separate arguments (e.g. text.Split(' ', '\n')).)

Lossa answered 2/3, 2010 at 22:15 Comment(3)
Split takes a params array, which means you can pass in an array, or you can simply pass in one or more character literals, without an array. text.Split ' ' or text.Split ' ', '\n' (with or without parens) are perfectly valid.Hammerhead
@Joel - Your second example doesn't work; it creates a tuple with '\n' as the second half. It does work when called as text.Split(' ', '\n'), though.Chokeberry
@Chokeberry - You're right, I misread the output in FSI. I was surprised when it seemed to work.Hammerhead
F
6

The function String.split is now defined in the F# Power Pack. You must add

#r "FSharp.PowerPack.dll";; 
#r "FSharp.PowerPack.Compatibility.dll";; 

See the errata for Expert F#: http://www.expert-fsharp.com/Updates/Expert-FSharp-Errata-Jan-27-2009.pdf

Get FSharp.PowerPack.dll here: http://fsharppowerpack.codeplex.com/

Fireside answered 15/7, 2010 at 8:24 Comment(0)
D
1

Using .NET member functions:

let count (text : string) =
  text.Split [|' '|]
  |> Seq.length

Or using FSharpx, which gives us more ergonomic free-functions...

paket.dependencies

source https://api.nuget.org/v3/index.json

nuget FSharpx.Extras 2.3.2

paket.references

FSharpx.Extras

Usage:

open FSharpx

let count (text : string) =
  text
  |> String.splitChar [| ' ' |]
  |> Seq.length

Note that F# Power Pack seems to be deprecated.

Diaconate answered 27/4, 2020 at 12:4 Comment(1)
FSharpx wasn’t working for me (with Fable). I’m now using String.split from the excellent FSharpPlus project: fsprojects.github.io/FSharpPlus/reference/…Pipeline
Q
0

Others have commented already that String.Split is a method, not a static function.

However, as I mentioned in this other answer here, they recently introduce a new syntax in f#8 for cases like this, so we don't have to write those small and convenient module functions wrapping object members.

It is called _.Property as a shorthand for (fun x -> x.Property), and can be done like this:

let count (text : string) =
  text
  |> _.Split(' ')
  |> Seq.length

There is a limitation though: you can only use this new syntax to replace a lambda that does only an atomic expression, i.e.:

An atomic expression is an expression which has no whitespace unless enclosed in method call parentheses.

That is why, for example, I had to write it like _.Split(' '), as it is atomic. It would have not worked as _.Split ' ' (notice the whitespace before the curried parameter).

Quillon answered 13/6 at 3:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.