Learning F# — Part 6— strings

Strings are a sequence of characters. For example, a word formed using the English alphabet. Strings are made up for characters. “Hello” is a string which is made up of 5 characters. For computers, the characters in a string must be encoded in order to store or manipulate them. ASCII is one such encoding. Unicode is another. Unicode has a wide coverage of characters and thus helps in storing strings made up of characters from another language or signs. We have used strings in our previous examples. F# strings are Unicode strings.

Open the Program.fs from the F# project you created last time. View that post here. Change the main function to the one below.

let main argv =
let s = "F# is cool"
printfn "%s" s

What we have done here is, we have created a string “F# is cool” and bound it to s. Then we print s on the console. If you have programmed in C before, you would notice that formatting of text is like its done in C. In the previous example we saw that we printed “Hello”. Here we want to print s, which is of the type string. %s is a placeholder for a string. So printfn knows that it has to replace it with the string. We give the string value as the next argument to the printfn function. So when you have %s in the quotes after printfn, it expects a string as another argument. That means if we pass in something else other than a string it may break. Let’s check.

let main argv =
let s = "F# is cool"
let num = 10
printfn "%s" num
0

Here I have created an integer and passed it as an argument to the printfn, when it expects a string. Run it and check what happens! The compilation fails and the compiler grumbles and sends you a compilation error saying it was expecting a string.

What if you want to print both s and num? You will have to do this

printfn "%s and I have %d beers" s num

This prints F# is cool and I have 10 beers. Notice that %d is a place holder for integers. We want %s to appear first and then %d, hence the order of s and num. This is a quick introduction to strings. Go ahead and check out the official F# language reference for string here.

String manipulation

We know that strings are sequence of characters. Each character in the sequence is assigned a position number. The position numbers start from 0. This position number is often called as index.

So using the same example as above

printfn "%s" s.[0..4]

This prints F# is. Every type is associated with its set of behaviours. You can access those behaviours with the dot operator. You notice here that we use the dot operator after s and use square brackets. This denotes that we are manipulating the sequence. Square brackets in programming is popularly used for sequences, for e.g. arrays. So here we say within the sequence, take only characters from index 0 to 4. Count the position numbers starting from 0 in your string. This will be result F# is. It results in 5 characters. Counting from 0 to 4. Notice that the space between # and i is also a character.

Here we’ll see how to combine to strings. Its just like adding 2 numbers.

let main argv =
let s = "F# is cool"
let superlative = "est"
let phrase = s + superlative + " language"
let num = 10
printfn "%s and I have %d beers" phrase num
0

Until next time. Cheers.