Strings

No discussion about a foundation class library would be complete without talking about the capabilities it provides for storing and manipulating strings. We have already covered some aspects of string manipulation, such as converting bytes to strings using various encoding schemes. In this section, let's look at some other important string manipulation classes provided by the .NET Framework.

The System.String class that we are used to by now stores a string of Unicode characters. This class offers many useful features, such as the following:

  • Obtaining the length of the string

  • Checking if two strings are equal

  • Comparing two strings for a lexical relationship

  • Concatenating one or more strings to create a new string

  • Replacing a substring with another

  • Splitting a string into an array based on a delimiter

  • Changing cases

  • Trimming characters from both ends of the string

The following code excerpt illustrates many of these features:

// Project Strings

// Length
String s1 = "hello";
Console.WriteLine(s1.Length);

// Equality
bool b = ("Hello" == "hello"); // false
b = ("Hello".Equals("hello")); // false

// Case-sensitive comparison
int val = String.Compare("Hello", "hello"); // 1

// Case-insensitive comparison
val = String.Compare("Hello","hello", true); // 0

// Compare substrings "ello." Case-sensitive
val = String.Compare("Hello", 1, "hello", 1, 4, false);// 0

// Concatenation
String s2 = String.Concat("Hello", " ", "World");

// Replace
String s3 = "How can I help you?";
String s4 = s3.Replace("I", "we");

// Split
String[] sArray = s3.Split(' '),

// Trim
String s5 = " blah ".Trim();
String s6 = " blah ".TrimEnd();

Formatting

It is also possible to format one or more objects into a textual representation. The String class provides a method, Format, to accomplish this. The following code excerpt illustrates its use:

// Project Strings

int i = 2;
int j = 4;
String s = "equal to";

// s1 = "2 and 2 is equal to 4"
String s1 = String.Format("{0} and {0} is {1} {2}", i, s, j);

The first parameter specifies the format string. Within this format string, an object that is passed as an argument to Format can be represented in the form {N}, where N represents the zero-based index of the argument.

Note that the format string used in Format is similar to that in Console.WriteLine. Internally, Console.WriteLine, and many other class methods that deal with formatted strings, end up calling String.Format for their formatting needs.

It is also possible to specify special formatting codes for an argument. This can be done using the representation {N:formatString} where formatString represents the string of formatting codes. Look in the SDK documentation for applicable formatting codes for base datatypes. The help topic labeled “Formatting Strings” is a good starting point. The following code excerpt, for example, formats an integer into a decimal (Base-10) representation. The minimum number of digits to display is 5:

// Project Strings

int i = 2;

// s2 = "00002";
String s2 = String.Format("{0:d5}", i);

How does String.Format know how to represent an object in its textual form? It doesn't. It relies on the object to return the string representation. System.Object.ToString should ring a bell here.

What about the formatting codes? System.Object.ToString doesn't seem to take the format string as a parameter.

Custom Formatting

A datatype that wishes to handle formatting beyond what System.Object .ToString offers must implement a standard interface, IFormattable. Here is its prototype:

public interface IFormattable {
     public String ToString(String format,
       IFormatProvider formatProvider) {
}

When an object needs to be formatted, the runtime first checks if the object implements IFormattable. If it does, then the runtime calls IFormattable.ToString on the object, passing the formatting information. Otherwise, it calls System.Object.ToString as usual.

In our earlier code, for example, System.Int32 implements IFormattable. Therefore, the runtime calls IFormattable.ToString on the object passing "d5" as the format string. This method knows how to deal with the formatting code.

Parameter formatProvider is used to format strings based on specific culture, and it is typically null when called by the runtime.

The following code excerpt shows how to call ToString directly to format an integer using the formatting code:

// Project Strings

int i = 2;

// s = "00002";
s = i.ToString("d5", null);

The following code excerpt shows how to implement the IFormattable interface. Here, method ToString on class Foo takes any formatting code and returns the same string in uppercase:

// Project Strings

class Foo : IFormattable {
     public String ToString(String format,
          IFormatProvider formatProvider) {
       return format.ToUpper();
     }
}

// Client code
Foo f = new Foo();
String s = String.Format("{0:abc}", f); // returns ABC

String to Base Datatypes

So far we have looked at representing objects, including the base datatypes, in their string form. How about converting a string to a base datatype?

The BCL provides a class, System.Convert, that defines static methods to convert strings to many base datatypes. For example, the following code excerpt converts a Base-10 string representation of a number to its integer form:

// Project Strings

int i = Convert.ToInt32("12", 10);

The System.Convert class defines many other static methods to convert one datatype to another. Check the SDK documentation for more information.

Mutable String Class

You may have wondered why the methods in the System.String class don't modify the string, but always return a new copy of the string. It's because although string is a reference type, it needs to be used as a value type.

Members that change the value of a class are called mutators, and a class that doesn't have any mutators is called an immutable class. Immutable classes are the way to create a class that behaves like a value class, but can't be written as a value class. Class String is an example of an immutable class.

There are situations in which it is desirable to modify a string without recreating a new string on each modification. For these situations, the BCL provides a class, StringBuilder (namespace System.Text). This class provides methods to remove, replace, and insert characters or strings to the existing string. The following code excerpt illustrates this:

// Project Strings

StringBuilder s = new StringBuilder("Hello");
s.Append(" Worldx"); // Hello Worldx
s.Replace('x', '!'), // Hello World!
String sNew = s.ToString();

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.130.13