Building a String

Introduction

We have mentioned that the string type is immutable. To give the ability to work on mutable strings, the .NET library provides a sealed class named StringBuilder. This class is defined in the System.Text namespace.

Both the String and the StringBuilder classes are used to build strings, but they do it differently and behave differently. In reality, the String and the StringBuilder classes have similarities and differences. These characteristics are demonstrated in the way a string is built and used in each of the classes.

Practical LearningPractical Learning: Introducing Text

  1. Start Microsoft Visual Studio and create a Console App (that supports .NET 8.0 (Long-Term Support)) named CountriesStatistics5
  2. On the main menu, click Project -> Add New Item...
  3. In the left list, under Visual C# Items, click Code
  4. In the middle list, click Interface
  5. Change the file name to Abbreviated
  6. Press Enter
  7. Change the document as follows:
    namespace CountriesStatistics5
    {
        internal interface IAbbreviated
        {
            string? Abbreviation { get; set; }
        }
    }
  8. In the Solution Explorer, right-click CountriesStatistics5 -> Add -> Class...
  9. Change the file Name to GovernmentEntity
  10. Click Add
  11. Create a string-based property as follows:
    namespace CountriesStatistics5
    {
        public abstract class GovernmentEntity
        {
            public virtual string? StateName { get; set; }
            public virtual int AreaSqrKms   { get; set; }
            public virtual string? Capital   { get; set; }
        }
    }
  12. In the Solution Explorer, right-click CountriesStatistics2 -> Add -> Class...
  13. Change the file name to State
  14. Click Add
  15. Change the class as follows:
    namespace CountriesStatistics5
    {
        public class State : GovernmentEntity,
                                IAbbreviated
        {
            public string ?Abbreviation { get; set; }
            public string[] SignificantCities { get; set; }
        }
    }
  16. In the Solution Explorer, right-click Program.cs and click Rename
  17. Type CountriesStatistics (to get CountriesStatistics.cs) and press Enter twice
  18. Change the CountriesStatistics.cs document as follows:
    using System.Text;
    using static System.Console;
    using CountriesStatistics5;
    
    State[] states = new State[6];
    
    string[] WesternAustralia = new string[3]
    {
        "Perth", "Ellenbrook", "Geraldton"
    };
    
    states[0] = new State() { Abbreviation = "WA ", StateName = "Western Australia", AreaSqrKms = 2529875, Capital = "Perth    " };
    states[0].SignificantCities = WesternAustralia;
    
    states[1] = new State() { Abbreviation = "SA ", StateName = "South Australia  ", AreaSqrKms = 983482, Capital = "Adelaide " };
    states[1].SignificantCities = new string[]
    {
        "Adelaide", "Mount Gambier", "Port Lincoln"
    };
    
    states[2] = new State()
    {
        Abbreviation = "QLD",
        StateName = "Queensland       ",
        AreaSqrKms = 1730648,
        Capital = "Brisbane ",
        SignificantCities = new string[] { "Brisbane", "Sunshine Coast", "Gold Coast" }
    };
    
    states[3] = new State()
    {
        Abbreviation = "NSW",
        StateName = "New South Wales  ",
        AreaSqrKms = 800642,
        Capital = "Sydney   ",
        SignificantCities = new string[] { "Sydney", "Newcastle", "Queanbeyan" }
    };
    states[4] = new State() { Abbreviation = "VIC", StateName = "Victoria         ", AreaSqrKms = 227416, Capital = "Melbourne" };
    states[5] = new State() { Abbreviation = "TAS", StateName = "Tasmania         ", AreaSqrKms = 68401, Capital = "Hobart    " };
    
    states[4].SignificantCities = new string[] { "Geelong", "Ballarat", "Bendigo" };
    
    // These are the significant cities for Tasmania
    states[5].SignificantCities = new string[3];
    states[5].SignificantCities[0] = "Devonport";
    states[5].SignificantCities[1] = "Hobart";
    states[5].SignificantCities[2] = "Strahan";
    
    WriteLine("Countries Statistics");
    WriteLine("==============================================================================================");
    WriteLine(" # Abbrv State Name           Area (kms)    Capital     Significant Cities");
    WriteLine("==============================================================================================");
    
    for (int counter = 0; counter < states.Length - 1; counter++)
    {
        WriteLine($" {counter + 1}  {states[counter].Abbreviation}  {states[counter].StateName}    {states[counter].AreaSqrKms,10}    {states[counter].Capital}   " +
        states[counter].SignificantCities[0] + ", " + states[counter].SignificantCities[1] + ", " + states[counter].SignificantCities[2]);
    
        WriteLine("----------------------------------------------------------------------------------------------");
    }
  19. To execute the application, on the main menu, click Debug -> Start Without Debugging:
    Countries Statistics
    ==============================================================================================
     # Abbrv State Name           Area (kms)    Capital     Significant Cities
    ==============================================================================================
     1  WA   Western Australia       2529875    Perth       Perth, Ellenbrook, Geraldton
    ----------------------------------------------------------------------------------------------
     2  SA   South Australia          983482    Adelaide    Adelaide, Mount Gambier, Port Lincoln
    ----------------------------------------------------------------------------------------------
     3  QLD  Queensland              1730648    Brisbane    Brisbane, Sunshine Coast, Gold Coast
    ----------------------------------------------------------------------------------------------
     4  NSW  New South Wales          800642    Sydney      Sydney, Newcastle, Queanbeyan
    ----------------------------------------------------------------------------------------------
     5  VIC  Victoria                 227416    Melbourne   Geelong, Ballarat, Bendigo
    ----------------------------------------------------------------------------------------------
    
    Press any key to close this window . . .
  20. Close the window and return to your programming environment

An Object to Build a String

To build a string, if you are writing your code in a class, you can include a using System.Text line in the top section of your document. Then, you can declare a StringBuilder variable.

To let you start building a string, the StringBuilder class is equipped with various constructors. If you want to work from scratch, use its default constructor. Here is an example:

using System.Text;

StringBuilder thoughts = new StringBuilder();

To let you work from a primary string, the class is equipped with a constructor that takes a string as argument. Its syntax is:

public StringBuilder(string value);

Here is an example of using this method to start building a string:

using System.Text;

StringBuilder Sociology;

Sociology = new StringBuilder("Human Interactions");

Primary Operations on String Building

Adding a String to a Builder

The primary operation to start a string consists of adding a character or a word to it. To support this operation, the StringBuilder class is equipped with an overloaded method named Append. This method has a version for every primitive type. For example, the version used to add an integer to a string uses the following syntax:

public StringBuilder Append(int value);

The version of the StringBuilder.Append() method used to add a string uses the following syntax:

public StringBuilder Append(string value);

The primary operation to start a string consists of adding a character or a word to it. To support this operation, the StringBuilder class is equipped with an overloaded method named Append. This method has a version for every primitive type. For example, the version to add a string uses the following syntax:

public StringBuilder Append(string value);

When you call the StringBuilder.Append() method, if the object is empty, such as if this is the first time you are calling the method on a variable declared using the default constructor, the argument would constitute the primary string of the variable. Based on this, here is an example of starting building a string:

using System.Text;

StringBuilder thoughts = new StringBuilder();

thoughts.Append("I think NFL Football players are probaly no different than lawyers in terms of attacking opponents and then after the event, shaking hands and going back to a civil normal life.");

Practical LearningPractical Learning: Appending a String

  1. In the above code, each property had the same number of elements in the array. Fortunately, each property can have a different number of elements. For examples, change the code as follows:
    using System.Text;
    using static System.Console;
    using CountriesStatistics5;
    
    State[] states = new State[6];
    
    string[] WesternAustralia = new string[3]
    {
        "Perth", "Ellenbrook", "Geraldton"
    };
    
    states[0] = new State() { Abbreviation = "WA ", StateName = "Western Australia", AreaSqrKms = 2529875, Capital = "Perth    " };
    states[0].SignificantCities = WesternAustralia;
    
    states[1] = new State() { Abbreviation = "SA ", StateName = "South Australia  ", AreaSqrKms = 983482, Capital = "Adelaide " };
    states[1].SignificantCities = new string[]
    {
        "Adelaide", "Mount Gambier", "Port Lincoln"
    };
    
    states[2] = new State()
    {
        Abbreviation = "QLD",
        StateName = "Queensland       ",
        AreaSqrKms = 1730648,
        Capital = "Brisbane ",
        SignificantCities = new string[] { "Brisbane", "Sunshine Coast", "Gold Coast", "Townsville", "Cairns", "Toowoomba" }
    };
    
    states[3] = new State()
    {
        Abbreviation = "NSW",
        StateName = "New South Wales  ",
        AreaSqrKms = 800642,
        Capital = "Sydney   ",
        SignificantCities = new string[] { "Sydney", "Newcastle", "Queanbeyan", "Tweed Heads", "Maitland", "Wollongong", "Albury" }
    };
    states[4] = new State() { Abbreviation = "VIC", StateName = "Victoria         ", AreaSqrKms = 227416, Capital = "Melbourne" };
    states[5] = new State() { Abbreviation = "TAS", StateName = "Tasmania         ", AreaSqrKms = 68401, Capital = "Hobart    " };
    
    states[4].SignificantCities = new string[] { "Melbourne", "Geelong", "Ballarat", "Bendigo", "Wodonga" };
    
    // These are the significant cities for Tasmania
    states[5].SignificantCities = new string[2];
    states[5].SignificantCities[0] = "Hobart";
    states[5].SignificantCities[1] = "Launceston";
    
    WriteLine("Countries Statistics - Australia");
    WriteLine("========================================================================================================");
    WriteLine(" # Abbrv State Name             Area (kms)  Capital     Significant Cities");
    WriteLine("========================================================================================================");
    
    for (int counter = 0; counter < states.Length - 1; counter++)
    {
        Write($" {counter + 1}  {states[counter].Abbreviation}  {states[counter].StateName}    {states[counter].AreaSqrKms,10}    {states[counter].Capital}   ");
    
        StringBuilder sbCities = new StringBuilder();
    
        for (int city = 0; city <= states[counter].SignificantCities.Length - 1; city++)
            sbCities.Append(states[counter].SignificantCities[city] + ", ");
    
        sbCities.Remove(sbCities.Length - 2, 2);
    
        WriteLine(sbCities.ToString());
        WriteLine("--------------------------------------------------------------------------------------------------------");
    }
  2. Te execute the project, on the main menu, click Debug -> Start Without Debugging:
    Countries Statistics - Australia
    ========================================================================================================
     # Abbrv State Name             Area (kms)  Capital     Significant Cities
    ========================================================================================================
     1  WA   Western Australia       2529875    Perth       Perth, Ellenbrook, Geraldton
    --------------------------------------------------------------------------------------------------------
     2  SA   South Australia          983482    Adelaide    Adelaide, Mount Gambier, Port Lincoln
    --------------------------------------------------------------------------------------------------------
     3  QLD  Queensland              1730648    Brisbane    Brisbane, Sunshine Coast, Gold Coast, Townsville, Cairns, Toowoomba
    --------------------------------------------------------------------------------------------------------
     4  NSW  New South Wales          800642    Sydney      Sydney, Newcastle, Queanbeyan, Tweed Heads, Maitland, Wollongong, Albury
    --------------------------------------------------------------------------------------------------------
     5  VIC  Victoria                 227416    Melbourne   Melbourne, Geelong, Ballarat, Bendigo, Wodonga
    --------------------------------------------------------------------------------------------------------
    
    Press any key to close this window . . .
  3. Close the window and return to your programming environment
  4. To involve functions, change the code as follows:
    using System.Text;
    using static System.Console;
    using CountriesStatistics5;
    
    State[] states = Create();
    
    Present(states);
    
    State[] Create()
    {
        State[] states = new State[6];
    
        string[] WesternAustralia = new string[3]
        {
            "Perth", "Ellenbrook", "Geraldton"
        };
    
        states[0] = new State() { Abbreviation = "WA ", StateName = "Western Australia", AreaSqrKms = 2529875, Capital = "Perth    " };
        states[0].SignificantCities = WesternAustralia;
    
        states[1] = new State() { Abbreviation = "SA ", StateName = "South Australia  ", AreaSqrKms = 983482, Capital = "Adelaide " };
        states[1].SignificantCities = new string[]
        {
            "Adelaide", "Mount Gambier", "Port Lincoln"
        };
    
        states[2] = new State()
        {
            Abbreviation = "QLD",
            StateName = "Queensland       ",
            AreaSqrKms = 1730648,
            Capital = "Brisbane ",
            SignificantCities = new string[] { "Brisbane", "Sunshine Coast", "Gold Coast", "Townsville", "Cairns", "Toowoomba" }
        };
    
        states[3] = new State()
        {
            Abbreviation = "NSW",
            StateName = "New South Wales  ",
            AreaSqrKms = 800642,
            Capital = "Sydney   ",
            SignificantCities = new string[] { "Sydney", "Newcastle", "Queanbeyan", "Tweed Heads", "Maitland", "Wollongong", "Albury" }
        };
        states[4] = new State() { Abbreviation = "VIC", StateName = "Victoria         ", AreaSqrKms = 227416, Capital = "Melbourne" };
        states[5] = new State() { Abbreviation = "TAS", StateName = "Tasmania         ", AreaSqrKms = 68401, Capital = "Hobart    " };
    
        states[4].SignificantCities = new string[] { "Melbourne", "Geelong", "Ballarat", "Bendigo", "Wodonga" };
    
        // These are the significant cities for Tasmania
        states[5].SignificantCities = new string[2];
        states[5].SignificantCities[0] = "Hobart";
        states[5].SignificantCities[1] = "Launceston";
    
        return states;
    }
    
    void Present(State[] administrations)
    {
        WriteLine("Countries Statistics - Australia");
        WriteLine("========================================================================================================");
        WriteLine(" # Abbrv State Name             Area (kms)  Capital     Significant Cities");
        WriteLine("========================================================================================================");
    
        for (int counter = 0; counter < administrations.Length - 1; counter++)
        {
            Write($" {counter + 1}  {administrations[counter].Abbreviation}  {administrations[counter].StateName}    {administrations[counter].AreaSqrKms,10}    {administrations[counter].Capital}   ");
    
            StringBuilder sbCities = new StringBuilder();
    
            for (int city = 0; city <= administrations[counter].SignificantCities.Length - 1; city++)
                sbCities.Append(administrations[counter].SignificantCities[city] + ", ");
    
            sbCities.Remove(sbCities.Length - 2, 2);
    
            WriteLine(sbCities.ToString());
            WriteLine("--------------------------------------------------------------------------------------------------------");
        }
    }
  5. To execute, press Ctrl + F5
  6. Close the window and return to your programming environment

The Value of a Built String

When you declare a StringBuilder variable, you get a String object. As a result, you can use a StringBuilder object where you would use a string value. For example, you can display its value in a console window. Here is an example:

using System.Text;
using static System.Console;

StringBuilder thought = new StringBuilder();

thought.Append("I think NFL Football players are probaly no different than lawyers in terms of attacking opponents and then after the event, shaking hands and going back to a civil normal life.");

WriteLine(thought);
WriteLine("====================================");

This would produce:

I think NFL Football players are probaly no different than lawyers in terms of a
ttacking opponents and then after the event, shaking hands and going back to a c
ivil normal life.
====================================
Press any key to continue . . .

Of course, you can also access a StringBuilder object declared in a class.

Converting a Built String to a String

One of the similarities between the String and the StringBuilder classes is that each of them holds a sequence of characters. In most cases, each of them can be used interchangeable, but not always. This depends on the context the variable of the String or the StringBuilder class is used. In the above example, we saw that the Console.Write() or the Console.WriteLine() method can be used to display the value of a StringBuilder variable, but a message box or a text box cannot display the value of a StringBuilder variable. Why?

Remember that both the String and the StringBuilder variable hold a sequence or array of characters. To support this, the overloaded Console.Write() and Console.WriteLine() methods have a version as follows:

public static void Write (char[] buffer);
public static void WriteLine (char[] buffer);

This means that, whether you pass a string or a StringBuilder value to a Console.Write() or Console.WriteLine(), these methods are equipped to handle and display a sequence or array of characters. If you ask another property or method to simply use a StringBuilder value, if that property or method is not directly equipped to handle a StringBuilder object, the property or method would not know what to do. To address this issue, the StringBuilder class overrides the ToString() method:

public override string ToString();

As a reault, whenever you need to use a StringBuilder value where you need a string, you can first convert the value to a string. Here are two examples:

using System.Text;
using static System.Console;

StringBuilder thought = new StringBuilder();

thought.Append("I think NFL Football players are probaly no different than lawyers in terms of attacking opponents and then after the event, shaking hands and going back to a civil normal life.");

WriteLine("Thought: " + thought.ToString());

This would produce:

Thought: I think NFL Football players are probaly no different than lawyers in terms of attacking opponents and then after the event, shaking hands and going back to a civil normal life.

Press any key to close this window . . .

As an alternative, you may want to create a string using some sections of a StringBuilder value. To support this, the StringBuilder class provides another version of the ToString() method. Its syntax is:

public string ?ToString (int startIndex, int length);

Adding Strings to a Builder

To add more than one string to a StringBuilder object, you can call the StringBuilder.Append() method. Here is an example:

using System.Text;
using static System.Console;

StringBuilder thought = new StringBuilder();

thought.Append("I think NFL Football players are probaly no different than " +
               "lawyers in terms of attacking opponents and then after the " +
               "event, shaking hands and going back to a civil normal life.");

thought.Append(" - Dr. Robert Huizenga");

WriteLine(thought.ToString());

This would produce:

I think NFL Football players are probaly no different than lawyers in terms of attacking opponents and then after the event, shaking hands and going back to a civil normal life. - Dr. Robert Huizenga

Press any key to close this window . . .

In the same way, you can keep calling the StringBuilder.Append() to add more strings, each call with the desired value. Here are examples:

using System.Text;
using static System.Console;

StringBuilder fields = new StringBuilder("Information Theory");

fields.Append("\n");
fields.Append("Databases");
fields.Append("\n");
fields.Append("Articfial Intelligence");
fields.Append("\n");
fields.Append("Computer Networking");
fields.Append("\n");
fields.Append("Graphics and Visualization");
fields.Append("\n");
fields.Append("Computer Security");
fields.Append("\n");
fields.Append("Cryptography");
fields.Append("\n");
fields.Append("Software Engineering");

WriteLine(fields.ToString());

This would produce:

Information Theory
Databases
Articfial Intelligence
Computer Networking
Graphics and Visualization
Computer Security
Cryptography
Software Engineering

Press any key to close this window . . .

Consider the following classes:

public interface IPolyhedron
{
    string ?Name        { get; }
    double FactorArea   { get; }
    double FactorVolume { get; }
    int Faces           { get; }
    int Edges           { get; }
    int Vertices        { get; }
}

public class Tetrahedron : IPolyhedron
{
    public double FactorArea   => 1.732051;
    public double FactorVolume => 0.117851;
    public string ?Name         => "Tetrahedron";

    public int    Faces    { get;  }
    public int    Edges    { get;  }
    public int    Vertices { get;  }
    public double Edge     { get; set; }

    public Tetrahedron(int faces, int edges, int vertices)
    {
        Faces    = faces;
        Edges    = edges;
        Vertices = vertices;
    }
}

public class Octahedron : IPolyhedron
{
    public int    Faces    { get;      }
    public int    Edges    { get;      }
    public int    Vertices { get;      }
    public double Edge     { get; set; }

    public double FactorArea   => 3.464102;
    public double FactorVolume => 0.471405;
    public string ?Name         => "Octahedron";

    public Octahedron(int faces, int edges, int vertices)
    {
        Faces = faces;
        Edges = edges;
        Vertices = vertices;
    }
}

Remember that the StringBuilder class provides an Append() method for every primitive type. This allows you to add all types of values to a StringBuilder object. You can also pass variables, fields, or properties that hold values. Here are examples:

using System.Text;
using static System.Console;

IPolyhedron poly = new Tetrahedron(4, 6, 4);
StringBuilder description = new StringBuilder();

description.Append("A ");
// Adding a string
description.Append(poly.Name);
description.Append(" is a polyhedron with ");
// Adding an integer
description.Append(poly.Faces);
description.Append(" faces, ");
description.Append(poly.Edges);
description.Append(" edges, and ");
description.Append(poly.Vertices);
description.Append(" vertices. The formula to calculate its surface area is Edge * Edge * ");
// Adding a double-precision floating-point number
description.Append(poly.FactorArea);
description.Append(" and its volume is approximately Edge * Edge * Edge * ");
description.Append(poly.FactorVolume);

WriteLine(description.ToString());
WriteLine("--------------------------------------------------------------------------------");
poly = new Octahedron(8, 12, 6);

description = new StringBuilder();

description.Append("An ");
// Adding a string
description.Append(poly.Name);
description.Append(" is a polyhedron with ");
// Adding an integer
description.Append(poly.Faces);
description.Append(" faces, ");
description.Append(poly.Edges);
description.Append(" edges, and ");
description.Append(poly.Vertices);
description.Append(" vertices. The formula to calculate its surface area is Edge * Edge * ");
// Adding a double-precision floating-point number
description.Append(poly.FactorArea);
description.Append(" and its volume is approximately Edge * Edge * Edge * ");
description.Append(poly.FactorVolume);

WriteLine(description.ToString());
WriteLine("================================================================================");

This would produce:

A Tetrahedron is a polyhedron with 4 faces, 6 edges, and 4 vertices. The formula to calculate its surface area is Edge * Edge * 1.732051 and its volume is approximately Edge * Edge * Edge * 0.117851
-------------------------------------------------------------------------------
An Octahedron is a polyhedron with 8 faces, 12 edges, and 6 vertices. The formula to calculate its surface area is Edge * Edge * 3.464102 and its volume is approximately Edge * Edge * Edge * 0.471405
================================================================================

Press any key to close this window . . .

Inserting a String

The StringBuilder.Append() method adds a string at the end of the existing string of the variable and produces a new string. The new string is considered a group (or array) of characters. This means that the produced string is not treated as a group of strings but as an array of characters. Each character or symbol occupies a specific position which is its index. The first character is positioned at index 0. The second character is positioned at index 1, and so on.

To let you add a character or string to the beginning, or to insert a character or a string at the beginning of the existing string, the StringBuilder class is equipped with an overloaded method named Insert. Each version of this method takes two arguments. The version used to insert a double-precision number uses the following syntax:

public StringBuilder Insert(int index, double value);

The version used to insert a string uses the following syntax:

public StringBuilder Insert(int index, string value);

The first argument is the index where you want the new value. The characters after that position would be pushed to the right. Here is an example:

using System.Text;
using static System.Console;

StringBuilder fields = new StringBuilder("Information Theory");

Title = "Computer Technology";
fields.Append(", ");
fields.Append("Databases");
fields.Append(", ");
fields.Append("Articfial Intelligence");
fields.Append(", ");
fields.Append("Computer Networking");
fields.Append(", ");
fields.Append("Graphics and Visualization");
fields.Append(", ");
fields.Append("Computer Security");
fields.Append(", ");
fields.Append("Cryptography");
fields.Append(", and ");
fields.Append("Software Engineering.");

fields.Insert(0, "Computational Science, ");

WriteLine(fields.ToString());

fields.Insert(54, "Distributed Systems, ");
WriteLine("-----------------------------------------------------------------------------");

WriteLine(fields.ToString());

WriteLine("=============================================================================");

This would produce:

Computational Science, Information Theory, Databases, Articfial Intelligence, Computer Networking, Graphics and Visualization, Computer Security, Cryptography, and Software Engineering.
-----------------------------------------------------------------------------
Computational Science, Information Theory, Databases, Distributed Systems, Articfial Intelligence, Computer Networking, Graphics and Visualization, Computer Security, Cryptography, and Software Engineering.
=============================================================================

Press any key to close this window . . .

Encoding and Decoding Text

Introduction

An application may have to deal with various types of values from different documents or communication means. The documents primarily contain text and such text can include various types of characters. A document may include some special symbols. Examples are < (the less than symbol), > (the greater than symbol), ' (the single quote), " (the double-quote), etc. Sometimes, those same symbols may be included in the text that must be sent. In some cases, you may want to publish some characters from various languages such as Greek, German, etc. If you don't do a good job, you may get corrupted or unpredictable results. To faithfully produce a document, you can make your application use some algorithms to analyze, check, and convert the characters somehow, or you can give your own special instructions to the application. You have various options.

Encoding Text

The .NET library provides various techniques to manipulate characters so they can be appropriately displayed in any application. This is mostly supported in the System.Text namespace. This namespace includes a class named Encoder. This class considers one character or an array of characters provided in Unicode format. The class would convert the character(s) in an appropriate format readable to the intended audience.

The primary class used to encode a character is named Encoding. This is an abstract class that receives a character as a byte type. The class analyzes such a character and produces the necessary result. Encoding is an abstract class. A class derived from it would do the actual job. Because human languages use different ways to represent their particular characters, the .NET library provides various classes for encoding.

Latin-based languages such as English primarily use 8 bits to represent their characters. To support this format, the .NET library provides a class named UTF8Encoding. This class is derived from Encoding.

To prepare to encode a character, declare a variable of type UTF8Encoding. The class is equipped with three constructors, including the default. Therefore, to start encoding, you can declare a UTF8Encoding variable using its default constructor. If you are working in a class, you can include the namespace in the top section of the document. Here is an example:

using System.Text;

public class Correspondance
{
    UTF8Encoding u8e = new UTF8Encoding();
}

To let you specify the character or the string to be encoded, the Encoding class is equipped with an overloaded method named GetBytes that its child classes inherit. This method provides versions that take a string, an array of characters, a sub-string from an existing string delimited by a starting index, and many oters. The version that takes a string uses the following syntax:

public virtual byte[] GetBytes(string s);

This method considers every one of the characters passed through the argument. It converts each character to its ASCII equivalent and produces an array that contains the ASCII numbers of the characters as items. Here is an example:

using System.Text;
using static System.Console;

UTF8Encoding u8e = new UTF8Encoding();

byte[] characters = u8e.GetBytes("Copyright &copy;, 2021");
        
for(int i = 0; i<characters.Length - 1; i++)
    WriteLine("Character: {0}", characters[i]);

WriteLine("=======================================================");

This would produce:

Character: 67
Character: 111
Character: 112
Character: 121
Character: 114
Character: 105
Character: 103
Character: 104
Character: 116
Character: 32
Character: 38
Character: 99
Character: 111
Character: 112
Character: 121
Character: 59
Character: 44
Character: 32
Character: 50
Character: 48
Character: 50
=======================================================
Press any key to continue . . .

Previous Copyright © 2001-2024, FunctionX Friday 15 October 2021 Next