Strings in C#

The String class in .Net has many capabilities. Below is a list of some string methods I use frequently in my development projects. Even when I have completed this list, I am sure it will over cover 5% of the capabilities within the String class. After reading this, I suggest you look over the MSDN description here.

String.Join()

I used String.Join recently to convert a Generic List of strings to a string delaminated with commas. I did it with this code:

string stringCommas = String.Join(", ", ListOfStrings.ToArray());

I started out by trying to create a foreach block that iterates through the list and then concatenate the values from the list seperated by a comma. Not only is this more complex, it utilizes much more of the systems resource. Each time you modify an existing String, it actually creates a new string in memory. If the Generic List had 500 strings, the foreach loop would have created ~500 strings in memory, each one a little larger than the previous one. Understanding this about strings, I knew there had to be a better way and the better way was with String.Join().

String.Format()

I have also used String.Format() a number of times. I needed to combine a string with an object. The object being the above String.Join() method. I used this code:

string string1 = "I have {0} computers.";
string Results = String.Format(string1, String.Join(", ", ListOfStrings.ToArray()));

Basically, the String.Format() lets you add some type of expression, like a String.Join or a mathmatical equation to a string.

String.IndexOf()

It is also common to try and find a specific word or value contained within another string. Again, without knowing the power within the string class, a wrong solution could be to loop through each word or letter within a string. Don’t do this, it is complex, resource intense and worst practice.

string String1 = GetValue(string Key);
string findIt = Parent.Name.ToLower().Substring(0, 3) + "." + child.Name;
if (String1.IndexOf(findIt) > -1)
{
     child.isChecked = true;
}

While populating a TreeView, I needed to check the saved TreeView collection for the default setting. If I found the findIt in the String1, then the checkbox within the TreeView was checked. Remember on this one that if the method returns 0 it means it found the string starting at the 1st letter. I have often made the mistake of checking for greater than 0 instead of -1. If the method return -1 it means it did not find an ocurrance of the string.

String.SubString()

I use this one a lot. It is common that I need only a part of a string when I am executing logic. The substring can be passed either a starting point or a starting point and a length. If you pass this method only the starting point, it will return the string from that character to the end of the string. Remember to start with 0 and not 1. If you pass a starting point and a length then the method will return the character from that starting point and for as many characters as the length specifies. You will receive an exception if the length exceeds the total length of the string you are using.

/If oldstring1 is "Ben Perkins" then newString1
//will be "Perkins"
string newString1 = oldString1.Substring(4);
 
//If oldstring2 is "Ben Perkins" then newString2
//will be "Ben"
string newString2 = oldString2.Substring(0, 3);

You can get fancy and combine multiple string methods together, for example use the string.IndexOf to return the starting point of the string.substring method.

//The newString will be
//"The Best C# Programmer in the World"
string old = "Ben Perkins, The Best C# Programmer in the World";
string newString = old.Substring(old.IndexOf("The"));

Again, stay away from for, while or foreach logic when working with strings, there are enough built in methods to do what is needed in a majority of cases.

String.Equals()

In many cases I see programmers using relational operators such as (==) or (!=) to check if 2 strings are the same. Both work and I haven’t done any analysis on how they would function differently, in regards to, performance or resource utilization. However I prefer the string.equals method over the if relational operator just because when I am working with a string I want to use the resources the string offers. Here is an example of the string.equals:

string string1 = "C# Programming rocks!";
if (string1.Equals("C# Programming rocks!"))
{
     string string2 = "I knew it all along.";
}
else
{
     throw new LiarLiarPantsOnFireException();
}

Download the source