Building a String

Introduction

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

Practical LearningPractical Learning: Introducing Text

  1. Start Microsoft Visual Studio
  2. On the main menu, click File -> New Project...
  3. In the New Project dialog box, make sure Console App (.NET Framework) is selected in the middle list.
    Change the Name of the project to Geometry14
  4. Click OK
  5. In the Solution Explorer, right-click Geometry14 -> Add -> New Item...
  6. In the Add New Item dialog box, in the left list, click Code
  7. In the middle list, click Interface
  8. Change the Name to Polyhedron
  9. Click Add
  10. Change the document as follows:
    namespace Geometry14
    {
        public interface IPolyhedron
        {
            string Name         { get; }
            double FactorArea   { get; }
            double FactorVolume { get; }
            int    Faces        { get; }
            int    Edges        { get; }
            int    Vertices     { get; }
        }
    }
  11. On the main menu, click Project -> New Class ...
  12. Type Tetrahedron as the name of the class
  13. Click Add
  14. Create the class as follows:
    namespace Geometry05
    {
        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;
            }
        }
    }
  15. On the main menu, click Project -> New Class ...
  16. Type Octahedron as the name of the class
  17. Click Add
  18. Create the class as follows:
    namespace Geometry05
    {
        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;
            }
        }
    }

An Object to Build a String

To build a string, if you are writing you 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;

namespace SocialSciences
{
    public class Reflection
    {
        public static void Main()
        {
            StringBuilder thought = new StringBuilder();

            return;
        }
    }
}

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;

namespace SocialSciences
{
    public class Sociology
    {
        public static void Main()
        {
            StringBuilder sociology = new StringBuilder("Human Interactions");

            return;
        }
    }
}

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);

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 become the primary string of the variable. Based on this, here is an example of starting building a string:

using System.Text;

namespace SocialSciences
{
    public class Reflection
    {
        public static void Main()
        {
            StringBuilder thought = new StringBuilder();

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

            return;
        }
    }
}

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 to the user. Here is an example:

using System.Text;
using static System.Console;

namespace SocialSciences
{
    public class Reflection
    {
        public static void Main()
        {
            StringBuilder thought = new StringBuilder();

            thought.Append("I think NFL Football players are probably 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("-----------------------------------------------------");
            WriteLine(thought);
            WriteLine("=====================================================");

            return;
        }
    }
}

This would produce:

Thought
-----------------------------------------------------
I think NFL Football players are probably 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 continue . . .

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

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;

namespace SocialSciences
{
    public class Reflection
    {
        public static void Main()
        {
            StringBuilder thought = new StringBuilder();

            thought.Append("I think NFL Football players are probably 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");
            WriteLine("-----------------------------------------------------");
            WriteLine(thought);
            WriteLine("=====================================================");

            return;
        }
    }
}

This would produce:

Thought
-----------------------------------------------------
I think NFL Football players are probably 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 continue . . .

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;

namespace SocialSciences
{
    public class Reflection
    {
        public static void Main()
        {
            StringBuilder thought = new StringBuilder();

            thought.Append("I think NFL Football players are probably 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("\n");
            thought.Append(" - Dr. Robert Huizenga -");

            WriteLine("Thoughts");
            WriteLine("-----------------------------------------------------");
            WriteLine(thought);
            WriteLine("=====================================================");

            return;
        }
    }
}

This would produce:

Thoughts
-----------------------------------------------------
I think NFL Football players are probably 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 continue . . .

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.

Practical LearningPractical Learning: Building a String

  1. In the Solution Explorer, right-click Geometry14 -> Add -> Class...
  2. Type Geometry as the name of the class
  3. Click Add
  4. Change the document as follows:
    using System.Text;
    using static System.Console;
    
    namespace Geometry05
    {
        public class Geometry
        {
            private static void Main()
            {
                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("Geometry - 3D - Polyhedrons");
                WriteLine("---------------------------------------------------------------");
                WriteLine("=-= Tetrahedron =-=");
                WriteLine(description + ".");
                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("=-= Octahedron =-=");
                WriteLine(description + ".");
                WriteLine("===============================================================");
            }
        }
    }
  5. To execute, on the main menu, click Debug -> Start Without Debugging:
    Geometry - 3D - Polyhedrons
    ---------------------------------------------------------------
    =-= Tetrahedron =-=
    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 appro
    ximately Edge * Edge * Edge * 0.117851.
    ---------------------------------------------------------------
    =-= Octahedron =-=
    An Octahedron is a polyhedron with 8 faces, 12 edges, and 6 vertices. The formul
    a to calculate its surface area is Edge * Edge * 3.464102 and its volume is appr
    oximately Edge * Edge * Edge * 0.471405.
    ===============================================================
    Press any key to continue . . .
  6. Press Enter to close the window and return to your programming environment

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;

namespace ComputerScience
{
    public class ComputerScience
    {
        public StringBuilder Fields;

        public ComputerScience()
        {
            Fields = new StringBuilder("Information Theory");
            Fields.Append(", ");
            Fields.Append("Databases");
            Fields.Append(", ");
            Fields.Append("Articifial 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");
        }
    }

    public class Orientation
    {
        public static void Main()
        {
            ComputerScience studies = new ComputerScience();
            StringBuilder description = new StringBuilder();

            WriteLine("Computer Science Fields");
            WriteLine("---------------------------------------------------------------");
            WriteLine("Computer Science Fields: " + @studies.Fields + ".");
            WriteLine("---------------------------------------------------------------");

            studies = new ComputerScience();

            studies.Fields.Insert(0, "Computational Science, ");

            WriteLine("Computer Science Fields: " + @studies.Fields + ".");
            WriteLine("---------------------------------------------------------------");
            studies.Fields.Insert(54, "Distributed Systems, ");

            WriteLine("Computer Science Fields: " + @studies.Fields + ".");
            WriteLine("===============================================================");

            return;
        }
    }
}

This would produce:

Computer Science Fields
---------------------------------------------------------------
Computer Science Fields: Information Theory, Databases, Articifial Intelligence,
 Computer Networking, Graphics and Visualization, Computer Security, Cryptograph
y, and Software Engineering.
---------------------------------------------------------------
Computer Science Fields: Computational Science, Information Theory, Databases, A
rticifial Intelligence, Computer Networking, Graphics and Visualization, Compute
r Security, Cryptography, and Software Engineering.
---------------------------------------------------------------
Computer Science Fields: Computational Science, Information Theory, Databases, D
istributed Systems, Articifial Intelligence, Computer Networking, Graphics and V
isualization, Computer Security, Cryptography, and Software Engineering.
===============================================================
Press any key to continue . . .

Encoding and Decoding Text

Introduction

As we saw in our introduction to characters, text can include various types of symbols or characters. A document may include some special symbols such as < (the less than symbol), > (the greater than symbol), ' (the single quote), " (the double-quote), etc. Sometimes, those symbols may be included in the text that must be transmitted. In some cases, you may want to publish some characters from various languages such as Greek, French, German, etc. If you don't do a good job, you may get corrupted or unpredictable results. To faithfully produce a document, you can use some algorithms to analyze, check, and convert the document somehow. You have various options.

Practical LearningPractical Learning: Introducing Text Encoding

  1. To start a new application, on the main menu, click File -> New -> Project...
  2. In the middle list of the dialog box, make sure Console App (.NET Framework).
    Change the name to PayrollPreparation5
  3. Click OK
  4. In the Solution Explorer, right-click Program.cs -> Rename
  5. Type AccountManagement to get AccountManagement.cs, and press Enter
  6. Read the text in the message box and click Yes

HTML Encoding

Encoding consists of checking a piece of text that must be transmitted so that some symbols or reserved characters must be changed without losing their meaning. One of the classes you can use to encode text is named Server.That class is available in the System.Web namespace. The class contains an overloaded method named HtmlEncode. The syntax of one of the versions is:

public static string HtmlEncode(string value)

As another technique, the System.Web namespace contains a class named HttpUtility. That class is available in the System.Web namespace as the HttpServerUtility class. The class contains an overloaded method named HtmlEncode. One of its syntaxes uses the following version:

public static string HtmlEncode(string value)

In both cases, pass a string as argument. The method would check every symbol in the string. If a symbol is a special character, it would be encoded. If the symbol is not a special character, it would be kept. At the end, the method returns a new string where special characters have been converted. Here is an example:

Practical LearningPractical Learning: Creating a Stream

  1. In the Solution Explorer, right-click References -> Add Reference...
  2. In the middle list of the Reference Manager text box, click the check box of System.Web

    String Interpolation

  3. Click OK
  4. Change the AccountManagement.cs document as follows:
    using System.Web;
    using static System.Console;
    
    namespace PayrollPreparation5
    {
        public class AccountManagement
        {
            public static int Main(string[] args)
            {
                string strEncodedUsername = string.Empty;
                string strEncodedPassword = string.Empty;
                string strOriginalUsername = "john&peter";
                string strOriginalPassword = "P@s$w>15But<20W0rd$";
    
                WriteLine("Employee Login");
                WriteLine("============================================");
                WriteLine("Username: " + strOriginalUsername);
                WriteLine("Password: " + strOriginalPassword);
                WriteLine("--------------------------------------------");
    
                strEncodedUsername = HttpUtility.HtmlEncode(strOriginalUsername);
                strEncodedPassword = HttpUtility.HtmlEncode(strOriginalPassword);
    
                WriteLine("Encoded Login");
                WriteLine("--------------------------------------------");
                WriteLine("Encoded Username: " + strEncodedUsername);
                WriteLine("Encoded Password: " + strEncodedPassword);
                WriteLine("============================================");
    
                return 0;
            }
        }
    }
  5. To execute, on the main menu, click Debug and click Start Without Debugging:
    Employee Login
    ============================================
    Username: john&peter
    Password: P@s$w>15But<20W0rd$
    --------------------------------------------
    Encoded Login
    --------------------------------------------
    Encoded Username: john&peter
    Encoded Password: P@s$w>15But<20W0rd$
    ============================================
    Press any key to continue . . .
  6. Press Enter to close the window and return to your programming environment

Encoding Text

The .NET Framework provides various techniques to manipulate characters so they can display appropriately in any application. This is mostly supported in the System.Text namespace. This namespace includes a class named Encoder. This class considers one symbol 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 a class that receives a character as a byte type. It analyzes it, 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 Framework provides various classes for encoding.

Latin-based languages such as US English primarily use 8 bits to represent their characters. To support this format, the .NET Framework 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;

public class Exercise
{
    public static void Main()
    {
        UTF8Encoding u8e = new UTF8Encoding();

        byte[] characters = u8e.GetBytes("Copyright &copy;, 2018");

        for (int i = 0; i < characters.Length - 1; i++)
            WriteLine(characters[i]);
        WriteLine("================================");
    }
}

This would produce:

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

HTML Decoding

Obviously when an encoded string gets to its destination, it must be restored to its original version. Decoding consists or restoring an encoded string. To support this operation, the HttpServerUtility class contains an overloaded method named HtmlDecode. The syntax of this method is:

public static string HtmlDecode(string value)

This time too, pass a string as argument. The method would try to identify every combination of characters that may represent a special character. When such a combination is encountered, the method would convert the combination to the appropriate symbol. For example, a combination of & would be converted to the ampersand.

Practical LearningPractical Learning: Decoding Text

  1. Change the document as follows:
    using System.Web;
    using static System.Console;
    
    namespace PayrollPreparation5
    {
        public class AccountManagement
        {
            public static int Main(string[] args)
            {
                string strEncodedUsername = string.Empty;
                string strEncodedPassword = string.Empty;
                string strDecodedUsername = string.Empty;
                string strDecodedPassword = string.Empty;
                string strOriginalUsername = "john&peter";
                string strOriginalPassword = "P@s$w>15But<20W0rd$";
    
    
                WriteLine("Employee Login");
                WriteLine("============================================");
                WriteLine("Username: " + strOriginalUsername);
                WriteLine("Password: " + strOriginalPassword);
                WriteLine("--------------------------------------------");
    
                strEncodedUsername = HttpUtility.HtmlEncode(strOriginalUsername);
                strEncodedPassword = HttpUtility.HtmlEncode(strOriginalPassword);
    
                strDecodedUsername = HttpUtility.HtmlDecode(strEncodedUsername);
                strDecodedPassword = HttpUtility.HtmlDecode(strEncodedPassword);
    
                WriteLine("Encoded Login");
                WriteLine("--------------------------------------------");
                WriteLine("Encoded Username: " + strEncodedUsername);
                WriteLine("Encoded Password: " + strEncodedPassword);
                WriteLine("--------------------------------------------");
                WriteLine("Decoded Login");
                WriteLine("Decoded Username: " + strDecodedUsername);
                WriteLine("Decoded Password: " + strDecodedPassword);
                WriteLine("============================================");
    
                return 0;
            }
        }
    }
  2. To execute, on the main menu, click Debug -> Start Without Debugging
    Employee Login
    ============================================
    Username: john&peter
    Password: P@s$w>15But<20W0rd$
    --------------------------------------------
    Encoded Login
    --------------------------------------------
    Encoded Username: john&amp;peter
    Encoded Password: P@s$w&gt;15But&lt;20W0rd$
    --------------------------------------------
    Decoded Login
    Decoded Username: john&peter
    Decoded Password: P@s$w>15But<20W0rd$
    ============================================
    Press any key to continue . . .
  3. Press Enter to close the window and return to your programming environment
  4. Close your programming environment

Previous Copyright © 2017-2019, FunctionX Next