Does F # have its own string manipulation libraries?

Does F # have its own string manipulation libraries?

As I try to learn F #, I found myself using existing System.string methods?

Should I do this?

Code:

open System type PhoneNumber = { CountryCode:int Number:string } // b. Create a function formatPhone that accepts a PhoneNumber record and formats it to look like something like this: "+44 1234 456789" let formatPhone phoneNumber = let getLeadingCharacters (length:int) (text:string) = text.Substring(0, length) let getLastCharacters (length:int) (text:string) = text.Substring(text.Length - length, length) printf "+%i %s %s" phoneNumber.CountryCode (phoneNumber.Number |> getLeadingCharacters 4) (phoneNumber.Number |> getLastCharacters 6) formatPhone { CountryCode=44; Number="123456789" };; 

UPDATE

Updated feature:

 let formatPhone phoneNumber = let getLeadingCharacters (length:int) (text:string) = text.Substring(0, length) let getLastCharacters (length:int) (text:string) = text.Substring(text.Length - length, length) printf "+%i %s %s" phoneNumber.CountryCode (phoneNumber.Number |> getLeadingCharacters 4) (phoneNumber.Number |> getLastCharacters 6) formatPhone { CountryCode=44; Number="123456789" };; 

in

 let formatPhone phoneNumber = printf "+%i %s %s" phoneNumber.CountryCode phoneNumber.Number.[0..3] phoneNumber.Number.[4..8] formatPhone { CountryCode=44; Number="123456789" };; 
+5
source share
2 answers

No, F # does not have a special String library that duplicates the .NET library. It has a string module with additional string functions.

Yes, use the .NET functions.

The fact that F # can use the entire .NET library is one of the most powerful features. At first it seems strange to mix functions using currency parameters with functions from .NET using tuple parameters.

This is why in NuGet you will see packages that also have the FSharp extension. e.g. MathNet Numerics and MathNet Numerics FSharp . These are wrapper functions that allow you to use the F # idiomatic function in the .NET library.

When looking for functions and methods to use with F #, I often use this trick. For .NET search, use class as a keyword and to search for F # code use module as a keyword.

For instance:

Google: MSDN string class
First element: String class

Google: MSDN string module
First Element: Core.String Module (F #)

+8
source

It depends.

If you use open source libraries such as FAKE for scripting, you can reuse the StringHelper module.

FAKE contains the StringHelper module https://github.com/fsharp/FAKE/blob/master/src/app/FakeLib/StringHelper.fs

0
source

Source: https://habr.com/ru/post/1242890/


All Articles