String type in C#.NET
We are having may inbuilt types like as Int,char,char[],string,string[]..etc. Today we are going to discuss the topic about the String Class in C#.NET.
A string class is a basic and most impotent type in .NET and it is derived from the name space System.String. We can say a string is a sequence of Unicode characters.
Declaring the string like as below.
protected void Page_Load(object sender, EventArgs e)
{
string firstName = "My First Name";
string lastName = "Last Name";
Console.WriteLine("My Name is {0}",firstName +" "+ lastName);
Console.WriteLine( );
}
Now the question comes into our mind is, a string is a value type or reference type?. We have a common misunderstand that string is a value type, because it is an immutable one. The answer for the above question is that the string is a reference type.Immutable:
Immutable is a class which behaves like a built in numeric type when you use the build in functions. We can say immutable types are reference types which is having a behavior of value types. We can say, once an instance is created, it cannot be modified. If methods are returning string values that mean new string objects will be created.
Below are the String functions which can modify the string or find the sub string..etc.IndexOf():
It is a string class method to find the substring index position in a main string. By using this you can find the substring existence and the Index of the starting point of a sub string.
string MainStr = "My First string";
string SecondStr = "str";
int Index = MainStr.IndexOf(SecondStr);
Console.WriteLine("Found at : " + Index.ToString());Trim():
It removes all the white space of a string at start and end point of a string. We have TrimEnd() and TrimStart() methods also.
class Program
{
static void Main(string[] args)
{
string MainStr = "My First string ";
string SecondStr= MainStr.Trim();
Console.WriteLine("Trimmed string : " + SecondStr);
}
}Replace():
It is strings method to replace the part of the string with a string. From the below code I am replacing the "My" from the main string with "Your" using the Replace method.
string MainStr = "My First string ";
string SecondStr= MainStr.Replace("My","Your");
Console.WriteLine("with Replaced string : " + SecondStr); Remove():
it is used to remove the part of the string by taking input as start point index and end point index positions in Remove function of a String class. Below is the example:
string MainStr = "My First string ";
string SecondStr= MainStr.Remove(1,3);
Console.WriteLine("Removed string : " + SecondStr); Split():
It is very useful function to split the string using the special characters like space,\,@..etc. We can split the string and assign to string array like as shown below. static void Main(string[] args)
{
string MainStr = "My First string ";
string[] SecondStr = MainStr.Split(new Char[] { ' ' });
Console.WriteLine("Splitted string : " + SecondStr[0].ToString());
}Difference between String and StringBuilder
1) String is class in C#.
2) Name space is System.String.
3) Strings are immutable; if you try to modify the string it actually creates a new string.
4) Once the string object is created, that string length and content cannot be modified.
5) The most common operation in String is Concatenation.
1) String Builder also a class in C#.
2) Name space is System.StringBuilder.
3) String Builder is mutable one.
4) Even after object is created, it can be able to modify length and content.
5) The most common Operation in String Builder is Append
This is very useful for freshers or beginners of C#.NET. We need articles more like this so that one can understand and use this can concepts in more better way.