Strings

1.Strings are character arrays in C#. 2.Strings are Immutable. 3.Strings are reference types. 4.Strings are simple data types in C#.

What is immutability

After execution string s="skill" in the following code, CLR will create a string with the data skill in string pool and the variable s stores address of the string. When the next line of code (s=s+"gun") is executed CLR will create a new string with the data skillgun in a new memory location (since data present in string pool is not modifiable). This is why we call string as an immutable entity. The variable s will store new strings address . Because of this immutability every time when we try to do an operation (modifying) on strings , a new string is created, old string will be still present in memory but is not accessible.
public class Test
{
    public static void Main()
    {
        string s = "skill";
        s = s + "gun";//Here + is doing concatenation operation not addition.
        Console.WriteLine(s);
    }
}
See the following image for more clarity.
string-before-modification
string-after-modification
Note:We can't modify data present in a string using array approach. Following code will produce compilation error since it is trying to modify data present in 3rd cell.
public class Test
{
    public static void Main()
    {
        string s = "skill";
        s[2] = 'u';
      /*the above code produce compilation error since it is trying to modify a character                present in the string (string content is not modifiable ). */
    }
}

Important string methods

You can see all methods present under string data type using the following approach. create a variable of type string , type variable name [dot] in visual studio to see all methods present under string data type. Following are the importent methods present under string data type.

Remove

Remove method deletes specified number of characters in a given string from given start position In the following program Remove method deletes data from third character to 5th character hence it gives "skl" as output.
Syntax: stringVariableName[dot]Remove(int startPosition,int count); or stringVariableName[dot]Remove(int startPosition);
public class Test
{
    public static void Main()
    {
        string s = "skill";
        string remainingData=s.Remove(2, 2);
        Console.WriteLine(remainingData);
    }
}

Replace

Replace method used for replacing a character or a string with another character or string from a given string. In the following sample first Replace method replaces char 'i' with char 'u' and in the second sample it replaces string "kil" with "wir" .
public class Test
{
    public static void Main()
    {
        string s = "skill";
        string modData=s.Replace('i', 'u');
        Console.WriteLine(modData);
        string Data2=s.Replace("kil","wir");
        Console.WriteLine(Data2);
    }
}

Contains

Contains method searches for existance of a character or a string in a given string .
public class Test
{
    public static void Main()
    {
        string s = "skill";
        bool isExists1 = s.Contains('z');//This method returns false since z is not existing in skill.
        bool isExists2 = s.Contains("kil");//This method returns true since kil is present in skill
        Console.WriteLine(isExists1);
        Console.WriteLine(isExists2);
    }
}

IndexOf

indexOf method returns the index number in which the specified string is present in the current string.
public class Test
{
    public static void Main()
    {
        string s = "skill";
        int position = s.IndexOf('i');//This method returns 2 since i is present in 3rd place or in 2nd index.
    }
}

SubString

Used for extracting a portion of string from the existing string.
public class Test
{
    public static void Main()
    {
        string s = "skill";
        string newString=s.Substring(1, 3);
        Response.Write(newString);//The output is kil since substring method retries characters
        //present in current string from 1st index place to 4th index         //place That is total 3 characters.
    }
}

Other Important methods and properties

NameTypeDescription
InsertMethodInsert a given character or string in the
LengthPropertyUsed for identifying total characters present in a given string

Assignments on strings

1.Write a program for displaying the characters present in the given string in ascending order. 2.Write a program for reversing the characters present in a given string. 3.Write a program for arranging charecters present in a string in ascending order.