Loading ...

How to manipulate the string in C# | CodeAsp.Net

How to manipulate the string in C#

 /5
0 (0votes)

Before start the string manipulation.first of all we take a review of String class in c#.

System.String class

  • String class is a sealed class and defined as "sequential collection of System.Char objects."

string vs String

  • string is used to represent text and it is sequential collection unicode characters.
  • String is used to represent string and it is sequential collection of System.Char objects.

String vs StringBuilder

  • String's value cannot be modify once it has been created.Methods that appear to modify a String actually return a new String which contains modified value.each time you have to create a object.
  • If you want to modify the actual content of a string like object just use the StringBuilder class.in string builder just append the value without creating the new object.

          I have given some example below:

            StringBuilder strbuild = "Hello";

            strbuild.Append("india");

            strbuild.Append("great");

Manipulation of String

  • Find String with in string

          string str= "Hello India";
     
      int location = str.IndexOf("nd");//Now location has the index of "nd" in str

  • Replace string with another string

          string str= "Hello Inida";

          string str1= str.Replace("Inida", "India");

  • Remove specified characters from a string

          string str= "Hello India";

          string str1= str.Remove(1,4);

  • Split string

      string str= "Hello.India";

          string [] str1= str.Split(new Char [] {'.'});

          // out put

          str1[0] contains: Hello

          str1[1] contains: India

 

Hope this will help.......

 

Comments (no comments yet)

Top Posts