String patterns in F #?

I'm new to F # and want to know if there is anything in F # that looks like template strings in Python . So I can just do something like:

    >>> d = dict(who='tim', what='car')
    >>> Template('$who likes $what').substitute(d)
    'tim likes car'
+4
source share
2 answers

The idiomatic way to do this in F # is sprintf:

let newString = sprintf "First Name: %s Last Name: %s" "John" "Doe"

In addition, you have access to .net String.Format:

let newString = String.Format("First Name: {0} Last Name: {1}", "John", "Doe")

The advantage of the first is that it is type safe (that is, you cannot pass a string to integer formatting, for example "% d"). As Benjol pointed out in the comments, it is not possible to pass a format string to sprintf, because it is statically typed. See here for more details .

+5
source

, , , sprintf:

let intro = sprintf "I am %d years old. My name is %s" 35 "Sam"
+1

All Articles