
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Appending a Line
|
79
Solution
Use the static IsNullOrEmpty method of the String class:
bool stringTestResult = String.IsNullOrEmpty(testString);
Discussion
The IsNullOrEmpty method is a very convenient method in that it allows you to test a
string for
null or zero length with a single method call. This method returns true if
the string passed in to it is equal to one of the following:
•
Null
• String.Empty
Otherwise, this method returns false.
See Also
See the “String.IsNullOrEmpty Method” topic in the MSDN documentation.
2.25 Appending a Line
Problem
You need to append a line, including a line terminator, to the current string.
Solution
Use the AppendLine method of the StringBuilder class:
StringBuilder sb = new StringBuilder("First line of string");
// Terminate the first line.
sb.AppendLine( );
// Add a second line.
sb.AppendLine("Second line of string");
This code will display the following:
First line of string
Second line of string
Discussion
The AppendLine method accepts a string and returns a reference to the same instance
of the
StringBuilder object on which this method was called. The string that is
passed in to this method has a newline character or characters automatically
appended on to the end of this string. The newline character(s) is dependent on the
type of platform you are running.