Manipulating Strings in C#—Creating Strings from Characters
Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio
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 strwherestris 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), and5is 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.
- 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. 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.titleis 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 ofchar).
- 2. Type
= new char[] {'C','S','H','A', 'R','P','V','Q','S'};newin 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 strwherestris the name of the variable to hold the string.
- 4. Type
= new string(title);wheretitleis the name of the array you declared in step 1.
- or
- Type
= new string(title,1,5);wheretitleis the name of the array you declared in step 1,1is the zero-based position of the first character you want from the array, and5is the number of characters to take from the array. In this examplestrwill 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.
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.

- Figure 4.32.
ToCharArrayis 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.
|

