String Handling Tips

0
21

String Handling Tips When working with strings, you should take advantage of certain classes and methods to avoid performance and memory problems. A key item to remember about .NET string is that they are immutable. Immutable means that strings are read-only and the value cannot be changed after it is created. If you add more data to the original string, a new instance of a string class is created to hold the new string and the old memory is marked for garbage collection. Thus, if you are doing a lot of string manipulation, you can create performance and memory issues in your application.

The StringBuilder Class for Concatenating

One way to avoid these issues is to use the StringBuilder class. The StringBuilder class is mutable so manipulations on strings are much quicker. You may pre-allocate space when creating an instance of the StringBuilder class. If you know you will be concatenating a lot of strings together pre-allocating space allows for growth without having to move memory blocks around. The methods Append, AppendLine and AppendFormat are what you will use to append new data into the StringBuilder object as shown below.

StringBuilder sb = new StringBuilder(1024);
sb.Append("Adding a new String without a line break.");
sb.AppendLine("Adding onto the previous line and add a line break");
sb.AppendFormat("Hello {0}", "Tom"); tbResult.Text = sb.ToString();

Other String Class Methods

A couple of other very useful techniques for working with strings are methods of the string class. The IsNullOrEmpty method helps you check to see if a string is a null value or an empty string. The use of this method is much better than using an “if” statement to check both. For example instead of 

1.<span class="Apple-tab-span" style="white-space: pre;">	</span>string value = null;
2.<span class="Apple-tab-span" style="white-space:pre"> </span>Debug.WriteLine(string.IsNullOrEmpty(value));
3.<span class="Apple-tab-span" style="white-space:pre">
</span>4.<span class="Apple-tab-span" style="white-space:pre"> </span>value = "";
5.<span class="Apple-tab-span" style="white-space:pre"> </span>Debug.WriteLine(string.IsNullOrEmpty(value));
6.<span class="Apple-tab-span" style="white-space:pre">
</span>7.<span class="Apple-tab-span" style="white-space:pre"> </span>value = " ";
8.<span class="Apple-tab-span" style="white-space:pre"> </span>Debug.WriteLine(string.IsNullOrEmpty(value));
9.<span class="Apple-tab-span" style="white-space:pre">
</span>10.<span class="Apple-tab-span" style="white-space:pre"> </span>value = "Some Text";
11.<span class="Apple-tab-span" style="white-space:pre"> </span>Debug.WriteLine(string.IsNullOrEmpty(value));

The results of running the above code is “True”, “True”, “False” and “False”. Notice that an empty space on line 7 is not an empty string. If you want to check for all white space within a string use the IsNullOrWhiteSpace as shown in the code snippet below:

string value = null;
Debug.WriteLine(string.IsNullOrWhiteSpace(value));
value = "";
Debug.WriteLine(string.IsNullOrWhiteSpace(value));
value = " ";
Debug.WriteLine(string.IsNullOrWhiteSpace(value));
value = "    ";
Debug.WriteLine(string.IsNullOrWhiteSpace(value));

The results of running the above code is “True”, “True”, “True” and “True”. Regardless of whether you have an empty string, a string with 1 space, or a string with several spaces the IsNullOrWhiteSpace method is perfect for checking user input. Some users may try to enter a lot of spaces to get around a required field. Using this method can help you ensure that spaces will not get by your requirement for a field that has actual data in it.

No matter how long you have been working with the .NET framework, you can always find something new to learn about. I hope these couple of tricks will help you in your programming.

LEAVE A REPLY