Home > Tutorial for Beginners > String Manipulation Functions in C#

String Manipulation Functions in C#


Replace: Replace method is used to change the substring in your string with another string or character set. Replace method is available in all programming language even the syntax is almost same in all.

 

    string str1 = "This string";

           string str2 = str1.Replace("This", "That String");
           Response.Write(str2);

Insert: It is used to append another string in your string.  This method does not change the value of your current string, even it returns a new string and new value is inserted in specified position.
 

string str1 = "This string";

Response.Write(str1.Insert(4,"Hello"));

Trim:  It is used to remove extra space (white spaces) from the string or occurrence of the string from both the side(right and left).
string str2 = " This is test ";
             Response.Write(str2.Trim() + "<br />");

You can pass the char array to remove special characters from your string.

 

string str1 = "#%@ This string ^@ ";

char[] specialChars = new char[] { ‘#’, ‘@’ };

             Response.Write(str1.Trim(specialChars) + "<br />");

Change Case: We can change the case of our string by using following methods

  • ToUpper(): Convert string to upper case.
  • ToLower(): Convert string to lower case.

String Compare: You can compare two string in asp.net by using string.compare method. In .Net you can compare your string by using additional parameters for eg. You can pass parameters of culture, case sensitivity. If it returns 0 that means both strings are matched and has the same value.
 

string str1 = "This string";

string str2 = "This string";

int result = string.Compare(str1, str2);

if(result ==0)
{
Response.Write("Both strings are same");
 

}


 

else

       {
           Response.Write("Strings are not matched");
       }

Startwith:It is Used to determine the string object whether it is start with a specified string or chars.If matches found it will return true otherwise will return false. You can pass some additional paratmers like true/false (for ignore case) & culture.

 

string myString = "String Sample";

       bool startsWithIgnoreCase = myString.StartsWith("this",true, null);

       Response.Write("Match Found [ignore case]?: " + startsWithIgnoreCase +"</br>");

Endwith:It is Used to determine the string object whether it is end with a specified string or chars.If matches found it will return true otherwise will return false. You can pass some additional parameters like true/false (for ignore case) & culture.

 

string myString = "String Sample";

       bool startsWithIgnoreCase = myString.EndWith("Sample",true, null);

       Response.Write("Match Found [ignore case]?: " + startsWithIgnoreCase +"</br>");

Concat:You can concatenate string and object into a single string. you can concatenate up to 4 strings or objects into a single string , otherwise you have to create object array or string array

 

Read Part 1: Tutorial on Strings in C#

A Must Read: Difference – string and String. How to decide which one should be used?

Post Comments Below

  1. No comments yet.
  1. No trackbacks yet.