F#'s System.Char Type Basics

Introduction

If you are coming from a .NET background and using C# most of the time you have probably encountered characters or Char type within the .NET space.

If your answer is yes, I assume you have used the System.Char including its common methods.

However, if your answer is no, no worries. In this article, we’ll discuss characters or Char type within the .NET using version 6 and the language F#.

We’re going to cover the following: how to define characters, character escape sequences, and how to represent a .NET character's Unicode value in a numerical value.

OK, let’s get started then.

How to Define Characters

One good thing to remember when we say characters. Just imagine that every time you declare a type Char, it stores a single text.

Now, a way to define a Char is assigned using single quotes around the literal value (character literal).

Of course, another way is by calling a function that returns a type, Char.

Let’s see some examples.

(*
    This example shows how to declare a character or System.Char type.
*)
[<Fact>]
let ``Declare Character`` () = 
    let letter = 'A'
    Assert.IsType(typedefof<char>, letter)
(*
    This example shows how to declare a character or System.Char type
    but by calling a function. 
*)
[<Fact>]
let ``Declare Character By Calling A Function`` () = 
    let funcReturnChar () = 'E' 

    let vowel = funcReturnChar()
    Assert.IsType(typedefof<char>, vowel)

That’s how easy it is, isn’t it?

Printing Emojis

Now let’s try to explore how we can print emojis.

But before that, I just want to emphasize that you can specify a Char value with a Unicode sequence which is \u, and then followed by the four-symbol hexadecimal representation of that certain character code.

OK, now let’s see those emojis in action. 😊

(*
    This example prints emojis. Yehey!
    Output: 
    Unicode Character 'White Smiling Face' (U+263A) = '☺'
    Unicode Character 'Right-Facing Armenian Eternity Sign' (U+058D) = '֍'
*)
[<Fact>]
let ``Print Characters`` () =
    (* a Unicode escape sequence, which is \u followed by the four-symbol hexadecimal representation of a character code. *)
    let smilingFace = '\u263A' (* ☺ *)
    let emoji1 = sprintf "Unicode Character 'White Smiling Face' (U+263A) = '%c'" smilingFace
    Assert.True(emoji1.Contains("☺"))
    output.WriteLine(emoji1)

    (* a Unicode escape sequence, which is \u followed by the four-symbol hexadecimal representation of a character code. *)
    let eternitySign = '\u058d'(* ֍ *)
    let emoji2 = sprintf "Unicode Character 'Right-Facing Armenian Eternity Sign' (U+058D) = '%c'" eternitySign
    Assert.True(emoji2.Contains("֍"))
    output.WriteLine(emoji2)

Just in case you missed it. Here's the output

Unicode Character 'White Smiling Face' (U+263A) = '☺'

Unicode Character 'Right-Facing Armenian Eternity Sign' (U+058D) = '֍'

Special Control Characters

For the special control characters, see the table below.

Character Meaning
\’ Single quote
\” Double quote
\\ Backslash
\b Backspace
\n Newline
\r Carriage return
\t Horizontal tab

Let’s see the example below on how we can play with the special control characters.

[<Fact>]
let ``Character Escape Sequences`` () = 
    
    let singleQuote = '\''

    Assert.True(singleQuote.ToString().Contains("'"))
    Assert.Equal(singleQuote, '\'')
    output.WriteLine(sprintf "Single Quote: %c" singleQuote)

    let doubleQuote = '\"'

    Assert.True(doubleQuote.ToString().Contains('"'))
    Assert.Equal(doubleQuote, '\"')
    output.WriteLine(sprintf "Double Quote: %c" doubleQuote)

    let backSlash = '\\'

    (* Both assert/checks are the same*)
    Assert.True(backSlash.ToString().Contains(@"\"))
    (* or *)
    Assert.True(backSlash.ToString().Contains("\\"))

    Assert.Equal(backSlash, '\\')
    output.WriteLine(sprintf "Backslash: %c" backSlash)

    let backSpace = '\b'

    Assert.True(backSpace.ToString().Contains("\b"))
    Assert.Equal(backSpace,'\b')
    output.WriteLine(sprintf "Backspace: %c" backSpace)

    let newline = '\n'

    Assert.True(newline.ToString().Contains("\n"))
    Assert.Equal(newline, '\n')
    output.WriteLine(sprintf "Newline: %c" newline)

    let carriageReturn = '\r'

    Assert.True(carriageReturn.ToString().Contains("\r"))
    Assert.Equal(carriageReturn, '\r')
    output.WriteLine(sprintf "Carriage Return: %c" carriageReturn)

    let horizontalTab = '\t'

    Assert.True(horizontalTab.ToString().Contains("\t"))
    Assert.Equal(horizontalTab, '\t')
    output.WriteLine(sprintf "Horizontal Tab: %c" horizontalTab)

Output

What's the Difference between /r and /r/n?

The main difference between the two is the platform. 

We have to remember that \r\n is for non-Unix platforms, while \n is for Unix platforms. 

In other words, Windows platforms use \r\n while Unix-related platforms use \n when creating a new line. 

See our example code below to prove our point. 

[<Fact>]
let ``Character NewLine in Different Platform`` () =

    (* If you are inside a Unix similar operating system*)
    if(RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
        RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || 
        RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) then 
        
        let newline = '\n'

        Assert.Equal(newline, System.Environment.NewLine[0])
        output.WriteLine(sprintf "Linux Machine's ('\\n) %c is equals %c(Environment.NewLine[0])." newline Environment.NewLine[0])
    else (* Windows *) 
        
        let newline = "\r\n"
        Assert.Equal(newline, System.Environment.NewLine)
        output.WriteLine(sprintf "Windows Machine's ('\\r'\\n) %s is equals %s(Environment.NewLine)." newline System.Environment.NewLine)

For the code sample above, we have to determine first the operating system then from there, we have proved the \r\n works for Windows while \n works for Unix-related platforms. 

Outputs

See the output for the Windows platform. 

See the output for the Ubuntu/Unix-related platform. 

Numeric Representation of .NET Character

Sometimes we wanted to get the numerical value of a certain character, especially when we wanted to check if the character is an ASCII. 

For us to easily understand this, let's see the example below. 

[<Fact>]
let ``Numeric Representation of Character`` () = 
    let letterZ = 'Z'
    
    Assert.True(Char.IsAscii(letterZ))

    let numericalValue = (int letterZ)

    Assert.IsType(typedefof<int>, numericalValue)
    Assert.Equal(numericalValue, 90)

Summary

In this article, we have discussed the following

  • How to Define Characters
  • Special Control Characters
  • Numeric Representation of .NET Character

I hope you have enjoyed this article, as I enjoyed it while writing. You can also find the sample code here on GitHub.

Till next time, happy programming!


Similar Articles