Strings and Formatting

let s = "Hello, F#"
s.Length                         // 9
s.ToUpper(), s.Substring(7), s.Contains("F#"), s.Replace("F#", "World")
s.[0]                            // 'H' (or s[0] in F# 6+)
s[7..]                           // "F#" (slicing)
"a,b,c".Split(',')               // [|"a"; "b"; "c"|]
String.concat ", " ["a"; "b"]    // "a, b"
String.replicate 3 "ab"          // "ababab"
String.map System.Char.ToUpper "abc"   // "ABC"
String.forall System.Char.IsDigit "123"   // true
"  padded  ".Trim()
System.String.Join("-", [|1; 2; 3|])     // "1-2-3"

// printf-family formatting (type-checked!)
printfn "%s is %d years old" "Ana" 30
printfn "%.2f" 3.14159           // 3.14
printfn "%A" [1; 2; 3]           // structured formatting of any value
printfn "%5d|%-5d|" 42 42
let msg = sprintf "%s-%d" "id" 7 // "id-7"
eprintfn "to stderr"

// String interpolation
let name, age = "Bo", 25
$"{name} is {age}"               // "Bo is 25"
$"{age:D3}"                      // "025" (.NET format specifiers)
$"%d{age} and %s{name}"          // typed interpolation (checked like printf)
$"""Quotes "inside" {name}"""    // triple-quoted interpolated
$$"""JSON: {"name": "{{name}}"}""" // extended interpolati
…