Strings are a part of any programming language, and C# has standard strings as well. Strings hold character values. They can also contain integers and decimal values, but you must convert them before the program can perform any calculations on them. C# has numerous string functions, classes and operations you can do on strings. They are much more complex since they have more than just standard mathematical operations. This articles covers strings and the .NET framework libraries that are used to work with them.
What is a String?
When you work with strings in C#, you assign at least one character to the variable. You can also assign multiple characters. Strings are not really a legitimate data type in any C language. Strings are actually an array of characters. The C# language (and other C derived languages) hides the process of turning a string of characters into the array. However, it's important to recognize that the string isn't actually stored in the same way we see it in the IDE.
In C#, there is also a difference between "string" and "String." The "string" data type is also called a primitive. It's a natural part of the compiler, so you can create a variable and work with the string. The "String" data type is a class. The String class is specific to C#, so it isn't found in any other language unless it's custom made by the developers. Some new developers immediately create strings with the String class, which is inefficient coding. The String class has several methods and properties that are useful for manipulating strings, but it should only be used if you absolutely need to use them.
Finally, the .NET framework as a class called StringBuilder. StringBuilder was created to address performance issues in C# when strings were concatenated and built dynamically in a program. For instance, if you have first and last name input from a user, you might want to concatenate these two input variables together to create a full name string. The StringBuilder class is recommended in this instance, because it is better with performance and resources than working with primitive strings.
Working with Primitive Strings
Primitive strings are natural data types in the C# language. Let's take a look at some code that illustrates the way a primitive string is used.
string fname, lname;
fname = "John";
lname = " Smith";
string fullname = fname + lname;
Console.WriteLine("Full Name: {0}", fullname);
Notice that the primitive string is automatically set to blue in the IDE. It's also all lowercase letters. This is important when distinguishing it from the String class.
The first line of code defines two string variables. They are then populated with the user's first and last name. In this example, the fname variable is set to "John" and the lname variable is set to "Smith." We then create a new variable named fullname and assign it the concatenated version of fname and lname. We mentioned that StringBuilder is the preferred method for concatenation, but we also want to illustrate how you can perform these actions using the primitive data type.
We put a space in front of the last name "Smith" because C# just combines the strings together without adding spaces. If we didn't add any spaces, the result would be "JohnSmith" and that's not user friendly output. Notice that we used the plus sign, which we covered in the operators chapter. When the compiler sees the plus sign used with two strings, it knows to concatenate the strings instead of perform mathematic calculations.
The final line of code displays the result to the user.
The above code is a simple primitive string example, but let's look at a string in action with a character array. Because a string is actually a character array, you can assign a character array to a string.
char[] fname = { 'J', 'o', 'h', 'n' };
string newname = new string(fname);
Console.WriteLine("Greetings: {0}", newname);
In the above code, we create a character array and fill it with the user's first name. Notice that we didn't use a string of characters. In a character array, each character is given a specific index in the array. This also happens with a string variable, but you don't have to work with the array and indexes when you create a string.
Notice that we then assign the newname variable the value of fname. We use the string function. It's important to note that when you assign a variable a value, you actually call the data type function such as the string function. This function combines the array's characters into a string and then assigns the value to the newname variable.
The final line of code displays the output to the user.
Using the String Class
The above code examples are the primitive string data type. You can't do much with the primitive type other than assign a string value and then store it or perform a few actions on it. You have many more options with the String class, but again you should only assign a string with the String class object type when you know that you need to perform several actions on the string.
The String class is a static class, so you don't need to instantiate it. This means that you can call its functions on-the-fly in your code. Let's take a look at some code samples that display the different methods and properties of the String class. There are several methods associated with this class, but we'll cover the most common ones you'll see in C# programming.
First, let's look at the Contains method. This method identifies if a string is contained within another string. Let's look at some sample code.
string text1 = "Test string 1";
if (text1.Contains("Test"))
{
Console.WriteLine("Test was found.");
}
In the above code, we create a string named text1. When then use the String class to call the method Contains. The Contains method identifies if a specific string of characters were found in the original string, which is text1 in this example. The method returns true or false. In this example, it returns true.
You might think that "test" and "Test" are the same string, but the letter "T" and "t" are different binary components. Had we tested for the string "test" in the Contain method, our if statement would return false. When you search for any text in a string, it's always case sensitive. Case sensitivity and using the Contains method often leads to frustrating bugs.
To overcome this issue, you can use the method "ToLower." You can also use the method "ToUpper." Both of these functions perform case sensitive actions. ToLower turns all letters into lower case characters. ToUpper does the opposite and returns all upper case letters.
When you work with the Contains method, you should use one of these methods and compare based on the case you choose. It eliminates logic errors in your code. Let's revise our code to reflect a better way to use the Contains method.
string text1 = "Test string 1";
if (text1.ToLower().Contains("test"))
{
Console.WriteLine("Test was found.");
}
Notice that we added the ToLower method prior to running the Contains method. We want the letters lowered before we search the string. We also changed the string we search for. It's now set to "test," because the text1 string is converted to "test string 1."
Obtaining a substring is also a common procedure in C# programming. You might want to obtain a part of a string and assign it to a new variable. You can extract a few characters from a string using the Substring method. Let's take a look at some sample code.
string text1 = "Test string 1";
string substr = text1.Substring(12);
Console.WriteLine(substr);
We again use the text1 string we used previously. This time, we use the Substring method on the string. This method extracts a part of the string starting with the character location you set in the function parameters. We indicated position 12, which is close to the end of the string. If you run the above code example in your Visual Studio IDE, the console displays "1" as the output. This is because the string is only 13 characters long, so only the last one is displayed.
Another common method you'll use in your C# programs is the Split method. The Split method cuts a string into several different strings and stores the result in a string array. You need a delimiter. A delimiter can be a comma, a letter, or just the space character. Let's take a look at some sample code.
string text1 = "Test string 1";
char delimiter = ' ';
string[] substrings = text1.Split(delimiter);
foreach (var substring in substrings)
Console.WriteLine(substring);
We use the original text1 variable to illustrate the Split function. We first define the string variable and then define the delimiter. The delimiter is just one character, and you must use this character in your Split function.
Notice that we have the data type "string[]" for the Split result. The [] designation after a data type declaration indicates that you want to create an array. The type of array is taken from the data type. In this example, the string[] data type tells the compiler to create a string array variable.
We assign the string array variable substrings the result of the Split function. Since we used the space character as the Split delimiter, the result is the following.
substrings[0] = Test
substrings[1] = string
substrings[2] = 1
We haven't covered arrays yet, but we will cover them in Chapter 7.
The final part of our code is the foreach loop. We'll also cover loops in Chapter 7. For now, just know that the loop iterates through each array value and prints it to the console output.
We showed you how to extract characters from a string, but sometimes you want to replace characters. You can replace characters with other characters or a blank string, which essentially removes them from the variable. Let's take a look at some sample code.
string text1 = "Test string 1";
string newstring = text1.Replace("Test", "Text");
Console.WriteLine(newstring);
We again use the original text1 string variable. We then use the Replace method to replace the phrase "Test" with the phrase "Text." The result from the Replace method is that newstring contains the value "Text string 1."
You can also use this method to remove characters from the string. Take a look at the following code.
string text1 = "Test string 1";
string newstring = text1.Replace("Test", "");
Console.WriteLine(newstring);
Notice that we changed the "Text" replacement with "", which is a zero-length string. This statement essentially removes "Test" from the string. The value of newstring is now " string 1." The Replace method doesn't automatically replace space characters, so the leading space would indeed remain since we didn't specify to the compiler that the first space should also be removed.
The final method we'll review is the Remove function. We removed characters in our string in the previous code, but the preferred method is using the Remove function. The Remove function has extra functionality that the Replace method doesn't have for removing characters.
Let's take a look at some code to illustrate how the Remove function works.
string text1 = "Test string 1";
text1 = text1.Remove(0, 4);
Console.WriteLine(text1);
In this example, we remove the first 4 characters from the string. With the Remove function, you provide integers for the start and stop sections of the string. Where Replace would only replace characters from a found match, the Remove function asks you for the start and stop location to remove the characters. Remember that a string is just an array of characters. The starting index location for any array is always 0, so we start the extraction at the first character, which is 0 for the string array.
The second parameter is the number of characters you want to remove. In this example, we tell the compiler to remove 4 characters, so the result from the function is " string 1" again. Again the space character is also present in the result, because we only removed the first four characters. If we specify 5 characters, the space is also removed and the result is "string 1."
The functions we covered are some common String class methods you'll need to program in C#. There are several others in the String class. You can review these methods by using the String class in Visual Studio.
Using StringBuilder
We mentioned that the StringBuilder class is the preferred method when appending strings. Concatenating strings using the primitive data type is inefficient, because the string is copied in memory each time the string is changed. This causes performance issues in your code. The answer to the problem is to use the StringBuilder class.
The StringBuilder class is not static like the String class. This means that you must instantiate it and store it in a variable to use it. The following code shows you how to instantiate a class.
StringBuilder MyStringBuilder = new StringBuilder("Test string 1");
In this example, the StringBuilder class is instantiated and stored in the variable MyStringBuilder. We also instantiate the class with the string "Test string 1." You can also instantiate the class with no string variable sent as a parameter. The following code is also valid.
StringBuilder MyStringBuilder = new StringBuilder();
Now let's append strings to the original string we created when we instantiated the code. This is the main reason StringBuilder is used. Appending data to existing strings is much more efficient using this class.
Let's look at some code that appends characters to the string.
StringBuilder MyStringBuilder = new StringBuilder("Test string 1");
MyStringBuilder.AppendLine();
MyStringBuilder.Append("Test string 2");
Console.WriteLine(MyStringBuilder.ToString());
The first line of code is the instantiation for the variable. The next line of code executes the AppendLine() method on the string. This is StringBuilder's own method that adds a new line character to the string. It's the same process as pressing the Enter key on your keyboard and creating a new line.
The next line appends the string "Test string 2" to the original string. StringBuilder now contains "Test string 1," a new line character, and then "Test string 2."
You'll notice that the final output statement uses the ToString() function on the StringBuilder variable. This function "flattens" the string to a primitive string, so you can output it to the user. Since the StringBuilder variable is a class variable, you can't print the output until you convert the class variable to the string data type.
Using strings is a huge topic that requires practice. As you work more with them, you'll get used to the different functions and methods you need to use to manipulate variables. Remember to use the StringBuilder class whenever you need to append any strings together to improve performance in your programs.