The. net Framework provides a set of powerful string functions. these building blocks can be used to write more complex algorithms for handling string data. however developers aiming to write fast and efficient string functions must be careful of how they use those building blocks. to write efficient string handling functions, it is important to understand the characteristics of string objects in C #.
String Characteristics First and foremost it is important to know that strings in. net are class objects. there is no difference between the types system. string and string, they are both class objects. unlike value types, class objects are stored in the heap (instead of the stack ). this is an important fact because it means that creating a String object can trigger garbage collection, which is costly in terms of performance. in terms of string functions, this means we want to avoid creating new strings as much as possible. however that is easier said than done. another important thing about strings in. net is that they are immutable. this means string objects cannot be modified. to edit a String object, you have to instead create a new string that will have the modification.
Working with characters The solution is to work with characters instead of strings as much as possible. the Char object in C # is a value type, which means all char variables are stored in the stack. furthermore, since a string is a collection of characters, converting between chars and strings is very simple. 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, simply create a new instance of a string: Char [] Mychars = {'h', 'E', 'l', 'l', 'O', '', 'w', 'O', 'R ', 'l', 'D '}; String Mystr =New String(Mychars ); Writing efficient string functions thus boils down to working with Char arrays. however you might remember that arrays are stored in the heap. thus there isn' t much difference between working with a string and a character array in terms of performance if we end up handling arrays in the same way as strings. yet this does not mean working with array is not faster. for one thing, we can make use of dynamic arrays such as List (or arraylist in. net Framework 1.1) to make our array management as efficient as possible.
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 StringCapitalizevowels (StringInput) { If(String. Isnullorempty (input ))// Since a string is a class object, it cocould be null Return String. Empty; Else { StringOutput =String. Empty; For(IntI = 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 } ReturnOutput; } } Using character Arrays: Public StringCapitalizevowels (StringInput) { If(String. Isnullorempty (input ))// Since a string is a class object, it cocould be null Return String. Empty; Else { Char[] Chararray = input. tochararray (); For(IntI = 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 ); } } Both functions will produce the exact same results given the same input data. we can perform some basic benchmarks to compare the performance of each function. for example, the string-based function took an average of 2181 ms to process the string "Hello World" 1,000,000 times while the array-based function only took 448 ms (measured on my computer ).
Conclusion As with anything, working with character arrays to write efficient string functions in C # Must be done with care. the code can be quickly become less readable. when working with more complex string algorithms, the code can become very difficult to maintain. however since the transition between working with strings and working with character arrays is easy, a combination of both can reach an advantageous middle ground.