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:
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:
- 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.
- 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:
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 string | Example |
using Literals | string str1; str1 = “Abhishek” |
Concatenating 2 strings | string s1 = “Abhishek”; string s2 = “Yadav”; string s3 = s1+s2; |
Reading inputs from Keyboard | string name = Console.ReadLine(); |
using ToString() method | int marks = 85; string sMarks = marks.ToString(); |
Examples
- //Ways of creating string;
- string name = "Abhishek"; //With help of Literals
- string surname = "Yadav";
- string fullName = name + surname; //Concatenating 2 strings
- Console.WriteLine("Full Name: {0}", fullName);
- Console.WriteLine("Enter your String:"); //Taking input from Keyboard;
- string text = Console.ReadLine();
- int marks = 65;
- string myMarks = marks.ToString(); // String created with ToString() method.
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.
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 | |
Method | Description |
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 | |
Method | Description |
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
- //Static Methods:
- Console.WriteLine("================= Static Methods ================\n");
- Console.WriteLine("Comparing \"abhishek\" and \"Sunny\" {0}",string.Compare("abhishek","Sunny"));
- Console.WriteLine("Copying text value in new string: {0}",string.Copy(text));
- Console.WriteLine("Is \"Apple\" and \"apple\" are equal ??: {0}",string.Equals("Apple","apple"));
- Console.WriteLine("Is text is NULL ? : {0}",string.IsNullOrEmpty(text));
- //All Instance Methods:
- Console.WriteLine("\n================= Static Methods ================\n");
- string cloneText = (string)text.Clone();
- Console.WriteLine("Clone Text: {0}",cloneText);
- bool containText = text.Contains("e");
- Console.WriteLine("Is text contains given value?: {0}",containText);
- string removedText = text.Remove(2);
- Console.WriteLine("New string after Removing: {0}",removedText);
- if (text.Contains("e"))
- {
- string replacedText = text.Replace("e","E");
- Console.WriteLine("New String after Replacing character: {0}", replacedText);
- }
- string insertedText = text.Insert(3, " Added String ");
- Console.WriteLine("Inserted Text: {0}",insertedText);
- string subString = text.Substring(3, 8);
- Console.WriteLine("New string after SubString(): {0}",subString);
1 comment :
That's nice
Post a Comment