Click here to Skip to main content
15,118,573 members
Articles / General Programming / String
Technical Blog
Posted 22 May 2017

Stats

3.8K views
2 bookmarked

String Handling Tips

Rate me:
Please Sign up or sign in to vote.
2.08/5 (4 votes)
22 May 2017CPOL2 min read
String handling tips

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.

C#
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:

C#
1.	string value = null;
2.	Debug.WriteLine(string.IsNullOrEmpty(value));
3.	
4.	value = "";
5.	Debug.WriteLine(string.IsNullOrEmpty(value));
6.	
7.	value = " ";
8.	Debug.WriteLine(string.IsNullOrEmpty(value));
9.	
10.	value = "Some Text";
11.	Debug.WriteLine(string.IsNullOrEmpty(value));

The results of running the above code are "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:

C#
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 are "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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Paul D. Sheriff
Employed (other) PDS Consulting
United States United States
Paul has been in the IT industry over 34 years. In that time he has successfully assisted hundreds of companies architect software applications to solve their toughest business problems. Paul has been a teacher and mentor through various mediums such as video courses, blogs, articles and speaking engagements at user groups and conferences around the world. Paul has 25 courses in the www.pluralsight.com library (https://www.pluralsight.com/author/paul-sheriff) on topics ranging from JavaScript, Angular, MVC, WPF, XML, jQuery and Bootstrap. Contact Paul at psheriff@pdsa.com.

Comments and Discussions

 
-- There are no messages in this forum --