Xceedas

Xceedas
xceedas

Tuesday 9 September 2014

Strings in C# .NET

Introduction
This article explains what strings are, including the types of strings, how to create strings, the String and StringBuilder classes themselves and will compare the performance of both of the classes.

As we all know, String Manipulation is the most common part of C# Programs. Strings represent a sequence of characters. C# supports the following 2 types of strings:
  1. Immutable Strings
    Immutable Strings are the strings whose content cannot be modified after the string has been created. And when we modify a string using string methods like Replace(), ToUpper(), ToLower() it seems that the string value has been changed, but actually it returns a new String object that contains the modified string value.
     
  2. Mutable Strings
    The content of Mutable Strings can be modified.
C# defines a predefined reference type known as string that is an alias of the System.String class in the Common Language Runtime (CLR).

The CLR maintains a table called “Intern Pool” that holds a single reference to each text value used by the program. A String variable that refers to a specific piece of text is actually a reference to the intern pool.

I'll talk more about the “Intern pool” and “String Pooling in C#” in my next article.

Using a string alias, we can create or declare string type objects. When we declare a string using the string type, we are actually declaring the object of type System.String class.

How to Create Strings
We can create immutable strings in many ways, as shown in the following table:
Ways of creating stringExample
using Literalsstring str1; str1 = “Abhishek”
Concatenating 2 stringsstring s1 = “Abhishek”; string s2 = “Yadav”; string s3 = s1+s2;
Reading inputs from Keyboardstring name = Console.ReadLine();
using ToString() methodint marks = 85; string sMarks = marks.ToString();
Examples
  1. //Ways of creating string;  
  2. string name = "Abhishek"//With help of Literals  
  3. string surname = "Yadav";  
  4. string fullName = name + surname; //Concatenating 2 strings   
  5. Console.WriteLine("Full Name: {0}", fullName);  
  6. Console.WriteLine("Enter your String:"); //Taking input from Keyboard;  
  7. string text = Console.ReadLine();  
  8. int marks = 65;  
  9. string myMarks = marks.ToString(); // String created with ToString() method.  
There're other ways also, I mean using string functions also we can create new strings.

String Methods
The String class provides various methods to play with your string. The following table will show some of the static as well as non-static methods provided by the String Class.
Static String Methods
MethodDescription
Compare(string string1m string string2)This method returns an int value, if string1 is smaller than string2 then it returns -1, if string1 is larger than string2 then it returns 1, if both strings are equal then it returns 0
Copy(string str1)Create a new string same as string1.
Equals(string str1, string str2)Returns true if both strings are equal.
IsNullOrEmpty(string str1)Returns true if string contains blank or null value.
IsNullOrWhiteSpace(string str1)Returns true if string contains blank, null or any whitespace characters.
The String class also provides some instance methods, please find the following table for that.
Instance String Methods
MethodDescription
Clone()Creates a new string. Same as Copy()
Contains(string value)Returns true if string contains specified value.
Remove(int startIndex)Removes the character from startIndex.
ToCharArray()Returns an array of char of given string.
Replace(char old, char new)Replace old letter/string with new specified one.
Insert(int startIndex, string newString)Insert new string at given index.
SubString(int startIndex, int length)Returns a new string from startIndex to given length.
Examples 
  1. //Static Methods:  
  2. Console.WriteLine("================= Static Methods ================\n");  
  3. Console.WriteLine("Comparing \"abhishek\" and \"Sunny\" {0}",string.Compare("abhishek","Sunny"));  
  4. Console.WriteLine("Copying text value in new string: {0}",string.Copy(text));  
  5. Console.WriteLine("Is \"Apple\" and \"apple\" are equal ??: {0}",string.Equals("Apple","apple"));  
  6. Console.WriteLine("Is text is NULL ? : {0}",string.IsNullOrEmpty(text));  
  7. //All Instance Methods:  
  8. Console.WriteLine("\n================= Static Methods ================\n");  
  9. string cloneText = (string)text.Clone();  
  10. Console.WriteLine("Clone Text: {0}",cloneText);  
  11.   
  12. bool containText = text.Contains("e");  
  13. Console.WriteLine("Is text contains given value?: {0}",containText);  
  14.   
  15. string removedText = text.Remove(2);  
  16. Console.WriteLine("New string after Removing: {0}",removedText);  
  17.   
  18. if (text.Contains("e"))  
  19. {  
  20.     string replacedText = text.Replace("e","E");  
  21.     Console.WriteLine("New String after Replacing character: {0}", replacedText);  
  22. }  
  23.   
  24. string insertedText = text.Insert(3, " Added String ");  
  25. Console.WriteLine("Inserted Text: {0}",insertedText);  
  26. string subString = text.Substring(3, 8);  
  27. Console.WriteLine("New string after SubString(): {0}",subString);  

1 comment :

Anonymous said...

That's nice