Chapter 15: Strings

Quiz

Solution to Question 15–1.

string (lowercase) is the C# keyword that maps to the .NET Framework String class. They may be used interchangeably.

Solution to Question 15–2.

IComparable

Guarantees that strings can be sorted

ICloneable

Guarantees that you can call the Clone method on a string object and get back a new duplicate string

IConvertible

Allows strings to be converted to other types (such as integers)

IEnumerable

Guarantees that strings can be iterated over in foreach loops

Solution to Question 15–3.

A quoted string, provided by the programmer, such as “Hello.”

Solution to Question 15–4.

An escape character embedded in a string indicates that the character or punctuation that follows is to be treated as an instruction rather than as part of the string. indicates a new line. " indicates that the quote symbol is in the string, not terminating it.

Solution to Question 15–5.

Verbatim strings are taken “as is” and thus do not require escape characters. Where \ would indicate a single backslash in a normal string, in a verbatim string, it indicates two backslashes.

Solution to Question 15–6.

Strings cannot be changed. When you appear to change a string, what actually happens is that a new string is created and the old string is destroyed by the garbage collector if it is no longer referenced.

Solution to Question 15–7.

It is not possible to derive from the String class (or any other sealed class).

Solution to Question 15–8.

You can call the Concat method of the String class, but it is more common to use the overloaded + operator.

Solution to Question 15–9.

Given an array of delimiters, Split( ) returns the substrings of the original string.

Solution to Question 15–10.

StringBuilder objects are mutable. When the StringBuilder has the complete set of characters you want, you call ToString( ) to get back a string object.

Solution to Question 15–11.

Regular expressions constitute a language for identifying and manipulating strings using both literal and metacharacters.

Exercises

Solution to Exercise 15-1.

Create the following six strings:

  • String 1: “Hello”

  • String 2: “World”

  • String 3 (a verbatim string): “Come visit us at http://www.LibertyAssociates.com"

  • String 4: a concatenation of strings 1 and 2

  • String 5: “world”

  • String 6: a copy of string 3

Once you have the strings created, do the following:

  1. Output the length of each string.

  2. Output the third character in each string.

  3. Output whether the character “H” appears in each string.

  4. Output which strings are the same as string 2.

  5. Output which strings are the same as string 2, ignoring case.

using System;

namespace StringManipulation
{
   class Tester
   {
      public void Run(  )
      {
         string s1 = "Hello ";
         string s2 = "World";
         string s3 = @"Come visit us at http://www.LibertyAssociates.com";
         string s4 = s1 + s2;
         string s5 = "world";
         string s6 = string.Copy( s3 );

         Console.WriteLine("Here's how long our strings are...");
         Console.WriteLine( "s1: {0} [{1}]", s1.Length, s1 );
         Console.WriteLine( "s2: {0} [{1}]", s2.Length, s2 );
         Console.WriteLine( "s3: {0} [{1}]", s3.Length, s3 );
         Console.WriteLine( "s4: {0} [{1}]", s4.Length, s4 );
         Console.WriteLine( "s5: {0} [{1}]", s5.Length, s5 );
         Console.WriteLine( "s6: {0} [{1}]", s6.Length, s6 );


         Console.WriteLine( "
Here's the third character in each string..." );
         Console.WriteLine( "s1: {0} [{1}]", s1[2], s1 );
         Console.WriteLine( "s2: {0} [{1}]", s2[2], s2 );
         Console.WriteLine( "s3: {0} [{1}]", s3[2], s3 );
         Console.WriteLine( "s4: {0} [{1}]", s4[2], s4 );
         Console.WriteLine( "s5: {0} [{1}]", s5[2], s5 );
         Console.WriteLine( "s6: {0} [{1}]", s6[2], s6 );

         Console.WriteLine( "
Is there an h in the string?" );
         Console.WriteLine( "s1: {0} [{1}]",
              s1.ToUpper(  ).IndexOf( 'H' ) >= 0 ? "yes" : "nope", s1 );
         Console.WriteLine( "s2: {0} [{1}]",
              s2.ToUpper(  ).IndexOf( 'H' ) >= 0 ? "yes" : "nope", s2 );
         Console.WriteLine( "s3: {0} [{1}]",
              s3.ToUpper(  ).IndexOf( 'H' ) >= 0 ? "yes" : "nope", s3 );
         Console.WriteLine( "s4: {0} [{1}]",
              s4.ToUpper(  ).IndexOf( 'H' ) >= 0 ? "yes" : "nope", s4 );
         Console.WriteLine( "s5: {0} [{1}]",
              s5.ToUpper(  ).IndexOf( 'H' ) >= 0 ? "yes" : "nope", s5 );
         Console.WriteLine( "s6: {0} [{1}]",
              s6.ToUpper(  ).IndexOf( 'H' ) >= 0 ? "yes" : "nope", s6 );

         Console.WriteLine( "
Which strings are the same as s2 [{0}]?", s2 );
         Console.WriteLine( "s1: {0} [{1}]",
              String.Compare( s1, s2 ) == 0 ? "Same!" : "Different", s1 );
         Console.WriteLine( "s2: {0} [{1}]",
              String.Compare( s2, s2 ) == 0 ? "Same!" : "Different", s2 );
         Console.WriteLine( "s3: {0} [{1}]",
              String.Compare( s3, s2 ) == 0 ? "Same!" : "Different", s3 );
         Console.WriteLine( "s4: {0} [{1}]",
              String.Compare( s4, s2 ) == 0 ? "Same!" : "Different", s4 );
         Console.WriteLine( "s5: {0} [{1}]",
              String.Compare( s5, s2 ) == 0 ? "Same!" : "Different", s5 );
         Console.WriteLine( "s6: {0} [{1}]",
              String.Compare( s6, s2 ) == 0 ? "Same!" : "Different", s6 );

         Console.WriteLine(
              "
Which strings are the same as s2 [{0}] ignoring case ?", s2 );
         Console.WriteLine( "s1: {0} [{1}]",
              String.Compare( s1, s2, true ) == 0 ? "Same!" : "Different", s1 );
         Console.WriteLine( "s2: {0} [{1}]",
              String.Compare( s2, s2, true ) == 0 ? "Same!" : "Different", s2 );
         Console.WriteLine( "s3: {0} [{1}]",
              String.Compare( s3, s2, true ) == 0 ? "Same!" : "Different", s3 );
         Console.WriteLine( "s4: {0} [{1}]",
              String.Compare( s4, s2, true ) == 0 ? "Same!" : "Different", s4 );
         Console.WriteLine( "s5: {0} [{1}]",
              String.Compare( s5, s2, true ) == 0 ? "Same!" : "Different", s5 );
         Console.WriteLine( "s6: {0} [{1}]",
              String.Compare( s6, s2, true ) == 0 ? "Same!" : "Different", s6 );
     }

      static void Main(  )
      {
         Tester t = new Tester(  );
         t.Run(  );
      }
   }
}
Solution to Exercise 15-2.

Take the following string:

We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness.

Use a regular expression to split the string into words:

using System;
using System.Text;
using System.Text.RegularExpressions;

namespace RegularExpressions
{
   class Tester
   {
      public void Run(  )
      {
         string importantString =
         "We hold these truths to be self-evident, " +
         "that all men are created equal, " +
         "that they are endowed by their Creator with certain " +
         "unalienable Rights, that among " +
         "these are Life, Liberty and the pursuit of Happiness.";

         Regex theRegex = new Regex( " |, |," );
         StringBuilder sBuilder = new StringBuilder(  );
         int id = 1;

         foreach ( string subString in theRegex.Split( importantString ) )
         {
            sBuilder.AppendFormat(
            "{0}: {1}
", id++, subString );
         }
         Console.WriteLine( "{0}", sBuilder );
      }

      static void Main(  )
      {
         Tester t = new Tester(  );
         t.Run(  );
      }
   }
}
..................Content has been hidden....................

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