Wednesday, 6 April 2016

string manipulations in F#

In previous posts we learned and got familiar with the theory and code of Functional programming language F#. The last post was mostly used operation of any language “The Print”. In this post we’ll use the mostly used type and literals in code i.e. String.
A string

String is an immutable data type in C# and F# is all about immutability. So you will not see much different between the C# string and F# string type. Means you’re already familiar with escape sequences\verbatim strings. Below is the list of supported escape sequences in F#:


Escape sequence





Carriage return






Quotation mark

\ "



Unicode character

\uXXXX or \UXXXXXXXX (where X indicates a hexadecimal digit)

The Verbatim string

Like in C# the verbatim string is ignore special character i.e. escape sequences. In F# it works with same symbol @ and special triple-quoted string.

// Using a verbatim string to represent xml node in string

let xmlFragment1 = @"<book author=""Milton, John"" title=""Paradise Lost"">"


// Using a triple-quoted string

let xmlFragment2 = """<book author="Milton, John" title="Paradise Lost">"""


Consider below examples:

let str1 = "Hello
let str2 = "Hello, \

The first Str1 is a string with new line character “Hello \n sir”. Linebreaks are actually produces the new line character in a string spanning across lines.

Str2 is a continuous string “Hello, Mam”. When a ‘\’ backslash character is the last character before the line breaks leading whitespaces are ignored and no line breaks will be considered.

String is array* of characters

Means you can access individual character with index like in an array*.

printfn "%c" str1.[1]

*The Array in F# will be covered in a dedicated post later.


The substring can be extracted from any string by supplying range in Index operator.

printfn "%s" (str1.[0..2]) // Substring from index 0 to 2
printfn "%s" (str2.[3..5]) // Substring from index 3 to 5

String as ByteArray (Ascii string)

// "abc" interpreted as a Unicode string.
let str1 : string = "abc"
// "abc" interpreted as an ASCII byte array. 
let bytearray : byte[] = "abc"B 

String operator

Just like C#, the ‘+’ operator concatenates two strings.

let string1 = "Hello, " + "world"

String Formatter

In the previous post we learned about the format specifiers. The all format specifiers can be used for string formatting. The equivalent of ‘String.Format’ in F# is ‘sprintf’

let formattedString = sprintf "The first letter of alphabet in %s is %c" "english" 'a'

Let’s run in F# interactive –

> formattedString;;

val it : string = "The first letter of alphabet in english is a"

The above are the basics of strings type in F#. Now let’s move to the interesting topic i.e. manipulation of strings. The string function are defined as module ‘Core.String’ under FSharp.Core namespace.

Library functions for string Manipulation

The list of function is available at above mentioned MSDN link. But here we’ll try to play a little with those function to understand what those functions actually do.

collect : (char -> string) -> string -> string

concat : string -> seq<string> -> string

exists : (char -> bool) -> string -> bool

forall : (char -> bool) -> string -> bool

init : int -> (int -> string) -> string

iter : (char -> unit) -> string -> unit

iteri : (int -> char -> unit) -> string -> unit

length : string -> int

map : (char -> char) -> string -> string

mapi : (int -> char -> char) -> string -> string

replicate : int -> string -> string


Builds a new string whose characters are the results of applying a specified function to each of the characters of the input string and concatenating the resulting strings.

let collectTesting inputS =

    String.collect (fun c -> sprintf "%c# " c) inputS


printfn "%s" (collectTesting "CFJ")

Output –

C# F# J#


Returns a new string made by concatenating the given strings with a separator.

let arr = [| "Sunday"; "Monday"; "Tuesday"; |]


let concatPlay = String.concat " comes after " arr

Output –

val concatPlay : string = "Sunday comes after Monday comes after Tuesday"


Tests if any character of the string satisfies the given predicate.

let containsUppercase string1 =
    if (String.exists (fun c -> System.Char.IsUpper(c)) string1) then
        printfn "The string \"%s\" contains uppercase characters." string1
        printfn "The string \"%s\" does not contain uppercase characters." string1
containsUppercase "Hello World!"
containsUppercase "no"


Output –

The string "Hello World!" contains uppercase characters.
The string "no" does not contain uppercase characters.


Tests if all characters in the string satisfy the given predicate.

let isValidName string1 =

    if (String.forall (fun c -> System.Char.IsLetter(c)) string1) then

        printfn "The string \"%s\" is a valid name." string1


        printfn "The string \"%s\" is not a valid name." string1

isValidName "Amit"

isValidName "Amit25"


Output –

The string "Amit" is a valid name.

The string "Amit25" is not a valid name.


Creates a new string whose characters are the results of applying a specified function to each index and concatenating the resulting strings.

let string1 = String.init 10 (fun i -> i.ToString())
printfn "%s" string1
let string2 = String.init 26 (fun i ->
    sprintf "%c" (char (i + int 'A')))
printfn "%s" string2


Output –



Applies a specified function to each character in a string.

let printCharacters sourceString =

    String.iter(fun c -> printfn "%c" c) sourceString

printCharacters "Happy New Year!!"

output –

















Similarly there are below function which takes lambda expressions(anonymous function) and applies on each character of given input.

map - Creates a new string whose characters are the results of applying a specified function to each of the characters of the input string.

iteri and mapi

iteri function applies a specified function to the index of each character in the string and the character itself and returns unit.

mapi function creates a new string whose characters are the results of applying a specified function to each character and index of the input string.


let enumerateCharacters inputString = 
    String.iteri (fun i c -> printfn "%d %c" i c) inputString
enumerateCharacters "TIME"
enumerateCharacters "SPACE"


Output –

Index       Unit

0       T
1       I
2       M
3       E
0       S
1       P
2       A
3       C
4       E


Returns a string by concatenating a specified number of instances of a string.

printfn "%s" <| String.replicate 10 "<>"

Output –



Returns the length of the string.

The sample included with each string library function are simplified as it’s a beginner level post. In upcoming posts we’ll see more complex examples with function piping, pattern matching etc.