Fsharp: Format String

By Xah Lee. Date: . Last updated: .

Sprintf

sprintf turns values into a string.

syntax:

sprintf string val1 val2 etc

First argument is a string, with placeholders. Placeholders start with the percent sign %.

// turn values into a string

// integer
let xx = sprintf "i have %i cats" 3
printfn "%s" xx
// i have 3 cats

// string
let yy = sprintf "this is string %s" "abc"
printfn "%s" yy
// this is string abc

// boolean
let zz = sprintf "this is %b" true
printfn "%s" zz
// this is true

Printf

The functions printf and printfn also take the same placeholders.

Format code

Reference