Operations on Text |
|
Building a String
Introduction
We have mentioned that the string type is immutable. To give 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.
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.
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; public class Exercise { public static int Main(string[] args) { 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; public class Study { public StringBuilder Sociology; public Study() { 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; public class Exercise { public static int Main(string[] args) { 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."); } }
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; public class Exercise { public static int Main(string[] args) { 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("===================================="); return 28_465; } }
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; using System.Text; using System.Windows.Forms; namespace StringsCharacteristics { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnQuotation_Click(object sender, EventArgs e) { 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."); string msg = thought.ToString(); txtThought.Text = thought.ToString(); MessageBox.Show(msg, "Quotations"); } } }
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; using System.Text; using System.Windows.Forms; namespace StringsCharacteristics { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnQuotation_Click(object sender, EventArgs e) { 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"); MessageBox.Show(thought.ToString(), "Quotations"); } } }
This would produce:
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;
using System.Text;
using System.Windows.Forms;
namespace StringsCharacteristics
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnQuotation_Click(object sender, EventArgs e)
{
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");
MessageBox.Show(fields.ToString(), "Computer Technology");
}
}
}
This would produce:
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; using System.Text; using System.Windows.Forms; namespace StringsCharacteristics { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnGeomtricVolumes_Click(object sender, EventArgs e) { 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); MessageBox.Show(description.ToString(), "Description"); poly = new Octahedron(8, 12, 6); description = new System.Text.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); MessageBox.Show(description.ToString(), "Description"); } } }
This would produce:
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; using System.Text; using System.Windows.Forms; namespace StringsCharacteristics { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnQuotation_Click(object sender, EventArgs e) { StringBuilder fields = new StringBuilder("Information Theory"); 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, "); MessageBox.Show(fields.ToString(), "Computer Technology"); fields.Insert(54, "Distributed Systems, "); MessageBox.Show(fields.ToString(), "Computer Technology"); } } }
This would produce:
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 Framework 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 Framework 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 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 int Main(string[] args) { UTF8Encoding u8e = new UTF8Encoding(); byte[] characters = u8e.GetBytes("Copyright ©, 2021"); for(int i = 0; i<characters.Length - 1; i++) WriteLine("Character: {0}", characters[i]); WriteLine("======================================================="); return 7_777; } }
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-2020, FunctionX | Next |
|