Creating Strings from Characters


Creating Strings from Characters

You can think of strings as collections of characters. The string class provides several mechanisms for creating strings from characters using the new operator. One way to create a string is by repeating the same character a certain number of times.

To create a string by repeating a character a number of times:

  1. Type string str where str is the name of the variable to hold the string.

  2. Type = new string('A',5) , where 'A' is any character (characters are represented in single quotes), and 5 is the number of times you wish to repeat that character. This form will create a string by repeating one character a specified number of times. ( Figure 4.29 )

    Figure 4.29 To save you from having to type all 25 asterisks , the string class provides an easy way to construct a string by repeating a character a number of times.
     string s = new string('*',25); Response.Write(s); //outputs: ************************ 

    Another way to create a string from characters is to first create an array of characters.

    We haven't talked about what an array is. An array is a fixed list of elements. Arrays will be discussed in detail in Chapter 9, "Arrays and Collections." The following instructions first show you how to create an array of characters, and then how to create a string from those characters.

To create a string from an array of characters:

  1. Type char[] title. title is a variable that can point to an array of characters. The way you tell C# that you want many characters (an array) instead of a single character is by putting square brackets after the data type ( char[] instead of char ).

  2. Type = new char[] {'C','S','H','A', 'R','P','V','Q','S'}; new in this case tells the compiler to create an array of characters and we can initialize each character in the array in place by putting the characters separated by commas inside curly brackets.

  3. Type string str where str is the name of the variable to hold the string.

  4. Type = new string(title); where title is the name of the array you declared in step 1.

    or

    Type = new string(title,1,5) ; where title is the name of the array you declared in step 1, 1 is the zero-based position of the first character you want from the array, and 5 is the number of characters to take from the array. In this example str will end up with the word "Sharp" ( Figure 4.30 ).

    Figure 4.30 Strings are essentially arrays of characters. Thus, the string class provides a way to create a string from a buffer.
     char[] title = new char[] {'C','S','H','A','R','P','V','Q','S'}; string str1 = new  string(title)  ; //contains CSHARPVQS string str2 = new  string(title,1,5)  ; //contains SHARP 

graphics/tick.gif Tip

  • You may be wondering whether these techniques for creating strings from characters are ever useful. Trust me, they are. The first technique is normally used for padding. Padding is when you need all your strings to be the same length because you want to display them in a table and you want each column to have a certain width. In this case you can add spaces at the end of the string by repeating the space character a number of times ( Figure 4.31 ). The second technique is useful when you want to create a string from part of another string. You can treat any string as a collection of characters; therefore, you can take some of the characters from the original string and use them to create a second string ( Figure 4.32 ).

    Figure 4.31 Constructing a string by repeating the same character a number of times is useful when you need to make all the strings the same size. In this case we are repeating the space character a number of times.
     string name1="John Smith"; string name2="Q"; string name3="Jackie Chan"; //make all names 40 characters long name1 +=  new string( ',40-name1.Length);  name2 +=  new string( ',40-name2.Length);  name3 +=  new string( ',40-name3.Length);  
    Figure 4.32 ToCharArray is a function in the string class that creates an array of characters from the characters in the string. The first parameter is the starting character index (zero based) and the second parameter is the number of characters to copy.
     string record= "SS#: 111-11-1111 Name: James T. Kirk"; string ssNum = new string(record.  ToCharArray(5,11)  ); 



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

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