- Preparing Your Machine to Work with Strings
- Working with Strings
- Initializing Strings
- Comparing Strings
- Concatenating Strings
- Finding the String Length
- Comparing and Concatenating Strings in the Sample Application
- Creating Strings from Characters
- Using Escape Characters
- Using Literal Strings
- Accessing the String's Characters
- Finding a Substring within a String
- Extracting Part of the String
- Splitting a String
- Joining a String
- Uppercasing and Lowercasing
- Formatting Strings
- Finishing the Sample Application
- Representing Objects as Strings
- Allocating Strings with StringBuilder
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:
Type string str where str is the name of the variable to hold the string.
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.
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:
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).
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.
Type string str where str is the name of the variable to hold the string.
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.
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 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.