Creating and Manipulating a String in C

Team-Fly    

 
.NET and COM Interoperability Handbook, The
By Alan Gordon
Table of Contents
Chapter Four.  A Quick Introduction to C#

Creating and Manipulating a String in C#

One of the most fundamental things that you will need to do as a computer programmer in any programming language is to work with strings: creating them, comparing them, searching them for subvalues, concatenating them, and parsing them. The biggest problem with string manipulation in C and C++ on the Windows platform is that there are so many ways to manipulate strings. The Windows platform supports strings with single-byte characters or wide characters or you can write code that is portable to both. There is a different set of string manipulation functions in the C and C++ runtime library for each of these character types. Of course, if you are using the MFC, it makes sense for you to use the CString class. However, if you do this, you will probably have to do many conversions to the BSTR string type if you are also using ActiveX controls or other COM objects. ATL has a string class called CComBSTR that wraps the BSTR string type. The native COM support in Visual C++ has its own class that wraps BSTR called _bstr_t. If you decide to use the Standard Template Library (STL), there is yet another string class called string, which is a generic (template) class that will work with both wide and single-byte characters. Each of these string classes has a completely different set of capabilities. The situation is an absolute mess!

With the .NET Framework, this problem is fixed. All programming languages that target the CLR will use the CTS string type called System.String. The System.String class stores its internal string using Unicode characters. It has a rich set of methods for manipulating a string. Table 4-5 shows a small sample of these methods. For a complete list and description of all the methods and properties in the string class, see the .NET Framework class library reference.

Table 4-5. A Sample of the methods in System.String

Method Name

Description

Clone

Returns a reference to the current string ( shallow copy).

Compare

Compares two strings with or without case sensitivity. Returns 1 if the first string is less (lexically) than the second string, 0 if the two strings contain the same value, 1 if the first string is greater than the second string.

CompareOrdinal

Similar to the Compare function, but performs its comparison without regard to local language or culture.

CompareTo

Similar to the Compare method, but unlike Compare, which is a static method, CompareTo compares the instance on which it is called against a specified string.

Concat

Concatenates two, three, or an array of strings into a single string.

Copy

Copies the contents of one string to another string.

Equals

Determines if two strings contain the same contents. The comparison is case sensitive.

Format

Replaces each format specification with the textual representation of an object's value. Similar to sprintf.

Indexof

Returns the index of the first occurrence of the specified string, character, or array of characters.

LastIndexof

Returns the index of the last occurrence of the specified string, character, or array of characters

Join

Joins an array of strings together with a specified separator between the strings.

Split

Creates an array of strings by splitting a string using a specified array of separators. This method is used for tokenization.

PadLeft

Right-aligns the characters in a string by padding on the left with a specified number of spaces or a character.

PadRight

Left-aligns the characters in a string by padding on the right with a specified number of spaces or a character.

Replace

Replaces all occurrences of a character with a specified replacement.

Substring

Returns a portion of a string starting at a specified index and continuing either to the end of the original string or to a specified length.

ToLower

Returns a copy of the string with all characters in lower-case .

ToUpper

Returns a copy of the string with all characters in upper-case .

Any type can be converted to a string using the toString function, which is defined on the System.Object class from which all other classes ultimately derive. Calling the toString method on many objects will return the name of the object's class, but some objects have a type-specific implementation of this method. For instance, most of the numerical types will return a textual representation of their underlying number. You can override the toString method in your types to return something else.

The string class also supports a Length property that returns the number of characters in a string and an equality operator "==" that allows you to test if two strings contain the same value; the equality operator is built on the Equals method, which is described in Table 4-5. The string class also contains an indexer method that allows you to return the individual characters in the string as follows :

 namespace StringApp {     using System;     public class TestStr     {     public static int Main(string[] args)     {       string str="Bria and Bryan";       for (int i=0;i<str.Length;i++)       {         Console.WriteLine(str[i]);       }          return 0;     }   } } 

However, strings are immutable, so don't try to execute the following code, which will cause an error:

 namespace StringApp {   using System;   public class TestStr   {     public static int Main(string[] args)     {       string str="Bria and Bryan";       str[5]='t';// compiler error       return 0;     }   } } 

You cannot modify a string after it has been created (I'll discuss this point in greater detail later). The string class also supports an addition operator, which you could use to concatenate two strings together as follows:

 namespace StringApp {   using System;   public class TestStr   {     public static int Main(string[] args)     {       string str;       str="Bria and Bryan" + " are playing";       Console.WriteLine(str);       return 0;     }   } } 

Later in this chapter, you will see why in some situations you are better off using the StringBuilder class instead.

The following code shows a sample program that demonstrates the use of some of these string functions. This program demonstrates some of the comparison functions like Compare, CompareTo, and Equals.

 namespace StringApp {   using System;   public class TestStr   {     public static int Main(string[] args)     {       string str1, str2;       int compResult;       bool equalsResult;       str1="Bria";       str2="Bryan";       // Code below returns 1.       // Bria is lexically less than Bryan       compResult=String.Compare(str1,str2);       Console.WriteLine("{0}",compResult);       // This will give the same result       // as the code shown above       compResult=str1.CompareTo(str2);       // Code below returns 1.       // Bria is lexically less than BRIA       compResult=String.Compare(str1,"BRIA");       Console.WriteLine("{0}",compResult);       // Code below returns 0, because the       // third parameter, "true", indicates       // that this is a case insensitive comparison       compResult=String.Compare(str1,"BRIA",true);       Console.WriteLine("{0}",compResult);       // Code below returns true       equalsResult=String.Equals(str2,"Bryan");       Console.WriteLine("{0}",equalsResult);       // Code below returns false       equalsResult=str2.Equals(str1);       Console.WriteLine("{0}",equalsResult);       // Code below returns false       equalsResult=(str1 == str2);       Console.WriteLine("{0}",equalsResult);       return 0;     }   } } 

The next example program demonstrates some of the searching and indexing methods supported by the string class as well as the use of the Format method.

 namespace StringApp {   using System;   public class TestStr   {     public static int Main(string[] args)     {       string str;       int indexResult;       bool searchResult;       str="Bria and Bryan are playing";     // The code below returns true       searchResult=str.EndsWith("ing");       Console.WriteLine("{0}",searchResult);     // The code below changes the string to:     // Bria and Bryan are laughing and playing       str=str.Insert(19,"laughing and ");       Console.WriteLine(str);     // The code below will return 5       indexResult=str.IndexOf("and");       Console.WriteLine("{0}",indexResult);     // The code below will return 28       indexResult=str.LastIndexOf("and");       Console.WriteLine("{0}",indexResult);     // The code below will return "laughing"       Console.WriteLine(str.Substring(19,8));     // The code below will print 3.433E+002       str=String.Format("The string is: {0:E3}",343.34);       Console.WriteLine(str);     // The code below will print 343.34       str=String.Format("The string is: {0:##.###}",343.34);       Console.WriteLine(str);     // The code below will print 343.340       str=String.Format("The string is: {0:##.000}",343.34);       Console.WriteLine(str);       return 0;     }   } } 

The last example program exercises the Join, Split, and Concat function.

 namespace StringApp {   using System;   public class TestStr   {     public static int Main(string[] args)     {       string str;       string str2Split1, str2Split2;       string[] strArray=new string[6];       string[] strSplitArray;       strArray[0]="A";       strArray[1]=".NET";       strArray[2]="Primer";       strArray[3]="For";       strArray[4]="COM+";       strArray[5]="Programmers";       str2Split1="The COM and COM+ ";       str2Split2="Programming Primer";       str=string.Join(" ",strArray); // Prints the string: "A .NET Primer For COM+ Programmers"       Console.WriteLine(str);       str=string.Concat(str2Split1,str2Split2); // Prints the string: "The COM and COM+ Programming Primer"       Console.WriteLine(str);       strSplitArray=str.Split(new char[] {' '});       foreach (string strSplitItem in strSplitArray)       {         Console.WriteLine(strSplitItem);         }       return 0;     }   } } 

Strings are immutable. In other words, after a string has been created, its contents cannot subsequently be changed. All of the string methods that appear to modify a string actually return a new instance of the string that contains the modification. For instance, on first glance, you would probably think that the following code would print the string "Bria and Bryan are laughing and playing".

 namespace StringApp {   using System;   public class TestStr   {     public static int Main(string[] args)     {       string str;       str="Bria and Bryan are playing";       str.Insert(19,"laughing and ");       Console.WriteLine(str);       return 0;     }   } } 

Well, you are wrong. If you compile and run this program, you will see that it prints the string "Bria and Bryan are playing". The call to Insert does not modify the string on which it is called; it returns a completely new string. Because we did not assign the return value of the Insert function to a variable, the result of the Insert function is lost. The correct way to write this program would be as follows:

 namespace StringApp {   using System;   public class TestStr   {     public static int Main(string[] args)     {       string str1, str2;       str1="Bria and Bryan are playing";       str2=str1.Insert(19,"laughing and ");       Console.WriteLine(str2);       return 0;     }   } } 

Creating new strings all the time will obviously degrade the performance of most applications. This is why Microsoft created the StringBuilder class. The StringBuilder class can be found in the System.Text namespace; it provides a string-like class that is more efficient to use when you are constructing a string by concatenating other strings together. The operations on the StringBuilder class operate on the current instance rather than generating a new string after each method call. The basic idea is that you assemble your string using the StringBuilder class and then you convert the string to a regular string when its contents are complete. The StringBuilder class contains the methods shown in Table 4-6:

Table 4-6. A sample of the methods in System.Text.StringBuilder

Method Name

Description

Append

Appends the string representation of an object to the StringBuilder instance on which it is called.

AppendFormat

Appends the string representation of an object to the StringBuilder instance on which it is called using a specified format string.

EnsureCapacity

Ensures that the capacity of the string is at least a specified integer value.

Insert

Inserts the string representation of an object at a specified location.

Remove

Removes the specified characters from the StringBuilder.

Replace

Replaces all occurrences of a character with a specified new character.

It also contains the properties listed in Table 4-7.

Table 4-7. A Sample of the Properties in System.Text.StringBuilder

Property Name

Description

Capacity

Sets or gets the number of characters that this StringBuilder can hold.

Chars

Gets or sets the character at the specified index in the string. This property performs the same function as the StringBuilder indexer directly below.

[ ]

The StringBuilder indexer. Unlike the String object, you can use this indexer to get or set a specific character in a StringBuilder instance.

Length

Gets or sets the length of the current StringBuilder.

MaxCapacity

Gets the maximum allowed capacity for the current StringBuilder (read-only).

The following example program demonstrates how to use the StringBuilder class to build up a string. If you had simply used the "+" operator on the string class to concatenate these strings, you would be creating a new string each time you used the "+" operator. In this example I built up a string using a single StringBuilder instance and then converted it to a string when I was done.

 namespace StringApp {   using System;   using System.Text;   public class TestStr   {     public static int Main(string[] args)     {       StringBuilder stb;       string str;       int orderID=12345;       decimal cost=4.54m;       stb=new StringBuilder("The total cost for order: ");       stb.AppendFormat("{0}", orderID);       stb.Append(" = ");       stb.AppendFormat("{0:C}",cost);       str=stb.ToString();   // Will print the following:   // The total cost for order: 12345 = .54       Console.WriteLine(str);       return 0;     }   } } 

The best thing about the System.String class is that all CLR-compliant programming languages share it. Therefore, whether you are using C#, VB.NET, JScript.NET, or any of a dozen other third-party languages that should be introduced around the same time as .NET, the way that you work with strings will be the same.


Team-Fly    
Top
 


. Net and COM Interoperability Handbook
The .NET and COM Interoperability Handbook (Integrated .Net)
ISBN: 013046130X
EAN: 2147483647
Year: 2002
Pages: 119
Authors: Alan Gordon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net