Team LiB
Previous Section Next Section

Strings As Objects

In Chapter 1, we introduced a number of predefined C# types, including the string type. What we hinted at, but didn't make explicitly clear at the time, is that strings are objects. We went over some of the basics of creating and using strings in Chapter 1; we'll now review some of what we've learned before, as well as provide additional insights about strings' object nature in this section.

The "string" Alias

The keyword string is really an alias for the String class defined in the System namespace. When we declare a string variable and assign it a value as follows:

string name = "Jackson";

we're in actuality instantiating an object/instance of the System.String class.

  • The expressions string and System.String are syntactically equivalent as far as the C# compiler is concerned; a string object has access to the methods, properties, and constructors declared in the System.String class.

  • Despite the fact that we don't see the use of the new operator to explicitly invoke a constructor of the System.String class, we're doing so nonetheless in "shorthand" fashion.

To reference the string type in our programs, we thus have several choices:

  • We can refer to the simple name of the class, String (uppercase "S"), if we include a using System; directive at the top of the program:

            using System;
    
            public class Foo
            {
              static void Main() {
                String s = "Whee!";
              }
            }
    
  • The fully qualified name System.String can be used, in which case it isn't necessary to provide the using System; directive at the top of the program:

            // No "using" directive needed!
    
            public class Foo
            {
              static void Main() {
                System.String s = "Whee!";
              }
            }
    
    Note?/td>

    Of course, the using System; directive is still necessary if we wish to access other System namespace elements, such as the Console class, by their simple names.

  • The preferred method in C# is to use the alias string (lowercase "s"). It provides a simpler syntax than System.String, but again enables us to omit a using System; directive at the top of the program:

    
            // No "using" directive needed!
    
            public class Foo
            {
              static void Main() {
                string s = "Whee!";
              }
            }
    

Given the convenience of the string alias, we virtually never use either the simple or fully qualified capitalized forms of String in a C# application.

Note?/td>

As it turns out, all of the predefined simple types discussed in Chapter 1bool, float, int, double, long, char, etc.—are in actuality aliases for elements defined in the System namespace.

Creating String Instances

As we learned in Chapter 1, we create a string instance by declaring a reference variable of type string and assigning it any valid string expression as a value:

string name = "Chen";

or

string name = student.Name;

or

string name = department.FindChairperson().Name;

etc.

A string can also be created using one of the overloaded String class constructors in conjunction with the new operator; the headers for some of the more commonly used String constructors are as follows:

public String(char[] characters)
public String(char c, int count)
public String(char[] characters, int start, int end)

As an example of using one of the String class constructors, we could do the following:


char[] chars = { 'C', 'h', 'e', 'n' };
string name = new String(chars);
Note?/td>

Note that, unlike Java, there is no constructor with a header that takes a single String argument:

      public String(String s)

e.g.,

      string s = new String("Fred"); // This doesn't exist in C#!

The @ Character

We learned in Chapter 1 that certain escape characters can be used to represent special characters such as tabs (\t) or newlines (\n) within a string literal. For example, if we wanted to output the following text to the console:

This line should break here:
and can contain backslashes (\), etc.

we could do so through the use of \n (newline) and \\ (backslash) escape characters as follows:

String str = "This line should break here:\n"+
             "and can contain backslashes (\\), etc.";
Console.WriteLine(str);

C# provides us with an alternative way of declaring such string literals without having to resort to the use of escape characters. If we precede the opening double quote mark of a string literal with @, the literal will be read "verbatim" from the source code file, allowing the literal to span multiple lines and leaving all newlines, tabs, backslashes, etc. intact.

Using the @ character, the previous code example could be rewritten exactly as follows:

    String str = @"This line should break here:
and can contain backslashes (\), etc";
    Console.WriteLine(str);

If the previous code snippet were executed, the desired output would be displayed to the console:


This line should break here:
and can contain backslashes (\), etc.

Special String Operators

As we learned in Chapter 1, the plus sign operator (+) concatenates string values:

string x = "foo";
string y = "bar";
string z = x + y + "!"; // z now has the value "foobar!"

The String class also provides specially defined versions of the == and != operators that can be used to compare the values of two strings for equality or inequality, respectively. The following code snippet demonstrates how these operators can be used:

string name = "Mary Jones";
if (name == "Cynthia Coleman") {
  Console.WriteLine("This is Cynthia Coleman");
}
else {
  Console.WriteLine("Hey! What happened to Cynthia?");
}

The previous code snippet would generate the following output:

Hey! What happened to Cynthia?
Note?/td>

We'll discover a bit later in this chapter that when we use the == and != operators to compare object references in general, the outcome is somewhat different.

String Properties

The String class also defines two useful properties.

The Length property returns the number of characters in the associated String, including white space characters:

string sentence = "How long am I?";
Console.WriteLine("Length = " + sentence.Length);

The output for the previous code snippet would be

Length = 14

Another useful feature of the String class is a special property called an indexer that allows the individual characters of a String to be accessed according to a character's position, or index, in the String. The use of an indexer is indicated by placing a pair of brackets around the desired index—any legitimate integer expression—thus mimicking the syntax of arrays:

String str = "Tom Servo";
Console.WriteLine("The first character is " + str[0]);

This code snippet would generate the following output:

The first character is T

String Methods

In addition to the operators and properties we've discussed so far, every string object has access to the methods declared by the String class. We'll discuss a few of the more useful methods in this section; for a complete description of all of the methods defined by the String class, please consult the FCL Reference on the MSDN web site.

  • public bool StartsWith(string str): Returns true if the string to which this method is applied starts with the value of the string expression provided as an argument, false otherwise.

          string s = "foobar";
          string t = "foo";
    
          // This will evaluate to true.
          if (s.StartsWith(t)) ...
    
  • public bool EndsWith(string str): Returns true if the string to which this method is applied ends with the value of the string expression provided as an argument, false otherwise.

          string s = "foobar";
    
          // This will evaluate to true.
          if (s.EndsWith("bar")) ...
    
  • public int IndexOf(string str): Returns a nonnegative integer indicating the starting character position (counting from 0) at which the value of the string expression provided as an argument is found within the string to which this method is applied, or a negative value if it isn't found.

          string s = "foobar";
          int i = s.IndexOf("bar"); // i will equal 3
    
          string t = "cat";
          int j = s.IndexOf(t); // j will be less than 0, because the
                                // value of the argument -- "cat" --
                                // is not found in "foobar".
    
  • public string Replace(char old, char new): Creates a new string object in which all instances of the old character are replaced with the new character—the original string remains unaffected.

          string s = "o1o2o3o4";
          // Note use of single quotes around character literals.
          string p = s.Replace('o', 'x'); // p now equals "x1x2x3x4",
                                          // while s remains "o1o2o3o4"
    
  • public string Replace(string old, string new): An overloaded version of the Replace method that allows us to replace one substring with another substring; note that the substrings need not be of equal length.

          string t = "foobar";
          string p = t.Replace("foo", "candy");   // p now equals "candybar"
    
  • public string Substring(int startIndex): Creates a new string whose value is based on a substring of an existing string, starting at the position in the existing string indicated by the int expression passed as an argument (counting from 0) and continuing through the end of the existing string:

          string s = "foobar";
          int i = 3;
          string p = s.Substring(i); // p now equals "bar"
    
  • public string Substring(int startIndex, int length): An overloaded form of the Substring method that creates a new string by taking a substring of an existing string, starting at the position indicated by the first int expression passed as an argument, and stopping just before the position indicated by the second int expression argument; again, we begin counting with 0 as the first character position.

          string s = "foobar";
          string p = s.Substring(1, 5); // p now equals "ooba"
    
  • public string ToLower(): Returns a copy of the string on which this method is applied, changing all of the characters to lowercase.

          string s = "Jose Cruz";
          string p = s.ToLower(); // p now equals "jose cruz"
    
  • public string ToUpper(): Returns a copy of the string on which this method is applied, changing all of the characters to uppercase.

          string s = "Jose Cruz";
          string p = s.ToUpper(); // p now equals "JOSE CRUZ"
    

Team LiB
Previous Section Next Section