Tuesday, 8 October 2013

How to Write Efficient String Functions in C#.Net

The .NET Framework provides a collection of powerful string functions. These building blocks is accustomed write additional advanced algorithms for handling string information. but developers about to write quick and economical string functions should watch out of however they use those building blocks.

To write economical string handling functions, it's necessary to know the characteristics of string objects in C#.

String Characteristics

First and foremost it's necessary to understand that strings in .NET square measure category objects. there's no distinction between the kinds System.String and string, they're each category objects. in contrast to worth sorts, category objects square measure keep within the heap (instead of the stack). this {can be} a vital truth as a result of it implies that making a string object can trigger trash pickup, that is dear in terms of performance. In terms of string functions, this implies we wish to avoid making new strings the maximum amount as attainable.

However that's easier same than done. Another necessary issue concerning strings in .NET is that they're immutable . this implies string objects can't be changed. To edit a string object, you've got to instead produce a replacement string that may have the modification.

Working with Characters

The solution is to figure with characters rather than strings the maximum amount as attainable. The char object in C# could be a worth kind, which suggests all char variables square measure keep within the stack. what is more, since a string could be a assortment of characters, changing between chars and strings is extremely easy.

To convert a string to a char array, use the ToCharArray() .NET function:


string myStr = “hello world”;
char[] myStrChars = myStr.ToCharArray();

To convert a char array back to a string, merely produce a replacement instance of a string:


char[] myChars = ;
string myStr = new string(myChars);

Writing economical string functions therefore boils all the way down to operating with char arrays. but you may bear in mind that arrays square measure keep within the heap. therefore there isn’t a lot of distinction between operating with a string and a personality array in terms of performance if we have a tendency to find yourself handling arrays within the same manner as strings.

Yet this doesn't mean operating with array isn't quicker. For one issue, we are able to build use of dynamic arrays like List (or ArrayList in .NET Framework one.1) to create our array management as economical as attainable.

Example Function
 
Let's write a very simple string function and compare the difference between using strings and char arrays. The function will capitalize all the vowels in a string (working with the English alphabet), and make all other characters lowercase.
 
Using just strings: 
 
public string CapitalizeVowels(string input)
{
    if (string.IsNullOrEmpty(input)) //since a string is a class object, it could be null
        return string.Empty;
    else
    {
        string output = string.Empty;
 
        for (int i = 0; i < input.Length; i++)
        {
            if (input[i] == 'a' || input[i] == 'e' ||
                input[i] == 'i' || input[i] == 'o' ||
                input[i] == 'u')
                output += input[i].ToString().ToUpper(); //Vowel
            else
                output += input[i].ToString().ToLower(); //Not vowel
        }
 
        return output;
    }
}
 
Using character arrays:
 
public string CapitalizeVowels(string input)
{
    if (string.IsNullOrEmpty(input)) //since a string is a class object, it could be null
        return string.Empty;
    else
    {
        char[] charArray = input.ToCharArray();
 
        for (int i = 0; i < charArray.Length; i++)
        {
            if (charArray[i] == 'a' || charArray[i] == 'e' ||
                charArray[i] == 'i' || charArray[i] == 'o' ||
                charArray[i] == 'u')
                charArray[i] = char.ToUpper(charArray[i]); //Vowel
            else
                charArray[i] = char.ToLower(charArray[i]); //Not vowel
        }
 
        return new string(charArray);
    }
}
 

0 comments:

Post a Comment