GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — F# String Tutorial - Comprehensive Guide to Strings in F# F# stringlast modified May 1, 2025 In this article we show how to work with strings in F#.
In F#, a string is a sequence of Unicode characters. Strings
are used to represent text and are a fundamental data type in F#. A string can
store letters, numbers, symbols, and even emoji characters. When a string
appears literally in the source code, it is called a string literal and
is enclosed in double quotes, for example:
Strings in F# are objects and are based on the .NET
The F# string simple exampleThe following is a simple F# string example. main.fsx
let w1 = "an old falcon" let w2 = "an" + " old" + " falcon" printfn "%s" w1 printfn "%s" w2 We define a regular string and print it, then we concatenate two strings. let w1 = "an old falcon" A string is delimited in two double quotes. let w2 = "an" + " old" + " falcon"
We concatenate three strings with the printfn "%s" w1
The string is printed to the terminal with λ dotnet fsi main.fsx an old falcon an old falcon F# string to int
The main.fsx
let vals = ("2", 1, "4", 6, "11")
let a, b, c, d, e = vals
let sum = int a + b + int c + d + int e
printfn "%d" sum
We have a tuple of values: integers and strings. We want to compute the sum of all values. let a, b, c, d, e = vals We destructure the tuple into five variables. let sum = int a + b + int c + d + int e
We sum the variables; the strings are converted into integers with
λ dotnet fsi main.fsx 24 F# string repeatIn the next example we show how to repeat a string in F#. main.fsx
printfn "%s" (String.replicate 5 "falcon ") printfn "%s" (String.concat " " (Array.create 5 "falcon"))
Strings can be repeated with $ dotnet fsi main.fsx Name: John Doe, Age: 34 F# string interpolationThe $ special character prefix identifies a string literal as an interpolated string. An interpolated string is a string literal that might contain interpolated expressions. main.fsx
open System
let name = "John Doe"
let occupation = "gardener"
let msg = $"{name} is an {occupation}"
printfn $"{msg}"
printfn $"5 * 8 = {5 * 8}"
let now = DateTime.Now
printfn $"Hello, {name}! Today is {now.DayOfWeek}."
We create a few interpolated strings. let msg = $"{name} is an {occupation}"
printfn "%s" msg
We build a string that contains the contents of two variables: printfn $"5 * 8 = {5 * 8}"
Interpolated strings can contain expressions. let now = DateTime.Now
printfn $"Hello, {name}! Today is {now.DayOfWeek}."
We interpolate a λ dotnet fsi main.fsx John Doe is an gardener 5 * 8 = 40 Hello, John Doe! Today is Monday. We can have typed format specifiers to enforce type safety. main.fsx
let name = "John Doe"
let age = 34
printfn $"Name: %s{name}, Age: %d{age}"
The F# string escape sequencesEscape characters are special characters that perform a specific operation. For instance, the \n characters starts a new line. main.fsx
printfn("Three\t bottles of wine")
printfn("He said: \"I love ice skating\"")
printfn("Line 1:\nLine 2:\nLine 3:")
We have an example with escape characters. printfn("Three\t bottles of wine")
The printfn("He said: \"I love ice skating\"")
We insert double quotes into a string literal by escaping them with \. printfn("Line 1:\nLine 2:\nLine 3:")
With λ dotnet fsi main.fsx Three bottles of wine He said: "I love ice skating" Line 1: Line 2: Line 3: F# string Contains
The main.fsx
let msg = "an old falcon"
let word = "falcon"
let r = msg.Contains("falcon")
if r then
printfn $"The string contains {word}"
else
printfn $"The string does not contain {word}"
In the program we check if the an old falcon string contains the falcon word. λ dotnet fsi main.fsx The string contains falcon F# string StartsWith/EndsWith
We can check if a string stars/ends with a character with
main.fsx
let words = ["sky"; "war"; "water"; "cup"; "cloud"; "warm"; "rock"; "try"] let res = words |> List.filter (fun e -> e.StartsWith 'w') printfn "%A" res let res2 = words |> List.filter (fun e -> e.EndsWith 'r') printfn "%A" res2 We have a list of words. We pick all words starting with w and then ending with r. λ dotnet fsi main.fsx ["war"; "water"; "warm"] ["war"; "water"] F# verbatim stringVerbatim strings do not interpret escape sequences. They are preceded with the @ character. We can create multiline strings with them. main.fsx
printfn "%s" @"deep \t forest"
printfn "%s" @"C:\Users\Admin\Documents"
let text = @"
Not marble, nor the gilded monuments
Of princes, shall outlive this powerful rhyme;
But you shall shine more bright in these contents
Than unswept stone, besmeared with sluttish time."
printfn "%s" text
In this code example we work with verbatim strings. printfn "%s" @"deep \t forest"
The printfn "%s" @"C:\Users\Admin\Documents" Verbatim strings are convenient when we work with paths. let text = @"
Not marble, nor the gilded monuments
Of princes, shall outlive this powerful rhyme;
But you shall shine more bright in these contents
Than unswept stone, besmeared with sluttish time."
Verbatim strings allow us to create multiline strings. λ dotnet fsi main.fsx
deep \t forest
C:\Users\Admin\Documents
Not marble, nor the gilded monuments
Of princes, shall outlive this powerful rhyme;
But you shall shine more bright in these contents
Than unswept stone, besmeared with sluttish time.
F# convert an array of ints to stringIn the next example, we convert an array of integers to a string. main.fsx
let nums = [| 2; 4; 6; 8 |]
let output =
nums
|> Array.map (sprintf "%i")
|> String.concat ","
printfn $"{output}"
We map the λ dotnet fsi main.fsx 2,4,6,8 F# building/formatting stringsThere are several ways how we can build or format strings. main.fsx
open System
open System.Text
let name = "John Doe"
let age = 33
let msg1 = name + " is " + string age + " years old"
printfn $"{msg1}"
let msg2 = sprintf "%s is %d years old" name age
printfn $"{msg2}"
let msg3 = $"{name} is {age} years old"
printfn $"{msg3}"
let msg4 = String.Format("{0} is {1} years old", name, age)
printfn $"{msg4}"
let builder = StringBuilder()
let msg5 = builder.AppendFormat("{0} is {1} years old", name, age)
printfn $"{msg5}"
The example builds five strings. let msg1 = name + " is " + string age + " years old"
printfn $"{msg1}"
A string can be concatenated with the let msg2 = sprintf "%s is %d years old" name age
printfn $"{msg2}"
We can use the let msg4 = String.Format("{0} is {1} years old", name, age)
printfn $"{msg4}"
Another option is the .NET String.Format function. let builder = StringBuilder()
let msg5 = builder.AppendFormat("{0} is {1} years old", name, age)
printfn $"{msg5}"
Finally, we can use the λ dotnet fsi main.fsx John Doe is 33 years old John Doe is 33 years old John Doe is 33 years old John Doe is 33 years old John Doe is 33 years old F# enumerate runes
The main.fsx
open System.Text
let text = "🐄🐘🐫🐑🦍🐯";
let runes = text.EnumerateRunes()
for rune in runes do
printfn $"{rune}"
We go over emojis inside a string literal. λ dotnet fsi main.fsx 🐄 🐘 🐫 🐑 🦍 🐯 In this article we have worked with strings in F#. AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |