 .NET 1.1+C# Basic String Operators
The seventeenth part of the C# Fundamentals tutorial looks at the use of the basic operators used earlier in the tutorial and how they can be applied to the string data type. These operators form the starting knowledge of C# string manipulation.
Concatenation Operator
Previously in this tutorial we examined the arithmetic operators for the numeric data types. The string data type does not contain any numeric value and therefore cannot be used in arithmetic operations. However, the addition operator (+) is available for string operations. This can be used to concatenate two strings into one longer string.
string start = "This is a ";
string end = "concatenated string!";
string concat = start + end; // concat = "This is a concatenated string!"
The concatenation operator, like the addition operation, can be used as a compound assignment operator with the addition of an equals sign (+=).
string start = "This is a ";
string end = "concatenated string!";
start += end; // start = "This is a concatenated string!"
Relational Operators
The string data type supports the use of just two relational operators. These are for equality (==) and inequality (!=) testing. The remainder of the relational operators are unavailable as they may only be used on data types that support the concept of ordering. It is important to note that the relational operators perform comparisons that are case-sensitive as shown in the following examples:
string s1 = "String to compare.";
string s2 = "String to compare.";
string s3 = "String to Compare."; // Note the capital "C"
bool result;
result = s1 == s2; // result = true
result = s1 == s3; // result = false
result = s1 != s2; // result = false
result = s1 != s3; // result = true
Null Coalescing Operator
In the previous part of the tutorial we introduced the string data type as a class and explained that all strings are actually objects. It was also noted that all objects are nullable. This means that the null coalescing operator (??) can be used with strings when developing under .NET framework 2.0.
string notNullString = "A string";
string nullString = null;
string nullTestString;
nullTestString = notNullString ?? "Null"; // nullTestString = "A string"
nullTestString = nullString ?? "Null"; // nullTestString = "Null"
The null coalescing operator is not available in earlier versions of the .NET framework. If you are developing in version 1.1 or earlier, the same functionality can be provided using the conditional and equality operators.
string notNullString = "A string";
string nullString = null;
string nullTestString;
// Mimic the functionality of the null coalescing operator
nullTestString = notNullString == null ? "Null" : notNullString; // "A string"
nullTestString = nullString == null ? "Null" : nullString; // "Null"
Summary
The number of basic operators available for use with strings appears to be very low. However, the data type is based on a class that includes many more powerful methods that allow manipulation of strings. These methods will be examined over the next articles in the tutorial.
|