- 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
Initializing Strings
When you declare a variable of type string, the runtime doesn't automatically create a buffer to hold that string. Like other reference types in which the actual object isn't created until you use the new operator, string buffers aren't created until you initialize the string.
To declare and initialize a string variable:
Type string str where str is the name of the variable to hold the string.
Type ="My String" where "My String" is the string you wish to store in the string variable declared in step 1.
Type ; (a semicolon) to end the statement (Figure 4.8).
Figure 4.8 Whenever you set a string variable to a literal value, you create a new string object in memory.
Tips
There are two ways of declaring string variables: You can use the C# keyword string or use the .NET Framework's class name System.String. Either way, you end up with the same thing—a string object. Having two ways of declaring a string object could be confusing in C#, especially because C# usually adds a using System; statement to each file. That means that you can omit the word System when specifying System.String. If you do so, then the two ways of declaring strings would be string (lowercase s) and String (capital s). So which one should you use? The C# ECMA specification says to use the language-provided keyword. However, other experts in the field say that you should use the class name from the Framework. I'm sticking with the language's reserved word, for consistency (Figure 4.9).
Figure 4.9 The class name for strings is System.String. However, the language provides an alias called string (lowercase letters). On top of that if you have a using statement for the System namespace you can also use String (capital S).
Before you initialize a string variable (setting it equal to the contents of a string), the variable is equal to null. Figure 4.10 shows code that tests whether the value of the variable is null.
Figure 4.10 The .NET Framework makes a distinction between uninitialized strings and empty strings. A string variable that hasn't been set to any literal value or to another string variable is null.
If you assign a different value to the string, the system silently creates a new string object (Figure 4.11).
Figure 4.11 Two girlfriends by definition can't share the same space. When you set the girlfriend variable to a different name, a new string object is created in memory and the variable points to the new object. The old string object is in memory until the system performs a garbage collection.
You can't break a string initialization statement into multiple lines unless you use literal strings (see "Using Literal Strings" later in this chapter) (Figure 4.12).
Figure 4.12 String literals can't be broken into two lines.