ASP.Net C# Replace String Function

Updated on 17 May 2012,
Published on 03 Jul 2008

C# Replace string function in ASP.Net provides the functionality to replace the part of a string with another string. In ASP.Net 2.0 C# replace function helps very much to replace the special characters from the string such as #, %, $, @ etc. C# Replace string function finds the specified string pattern in the target string and replaces its each occurrence with other string value specified in the function. C# Replace string function can be applied to the values of string data type. Following are the two types of overloads of Asp.Net C# Replace string function:

1. public string Replace(char oldChar, char newChar);

2. public string Replace(string oldValue, string newValue);

String Functions Examples:

You can see the live samples and examples of String functions from the following links:

First C# replace string overloaded function accepts two parameters of char type. First parameter takes the value for the character to be replaced in the string and second parameter takes the value as a new character to be placed in place of old character. Second C# replace string overloaded function also accepts two parameters of string type. First parameter takes the value as string pattern to be replaced from the target string and second parameter accepts the new string to be placed in place of old string.

Examples of ASP.Net C# Replace Function

Example 1
string strText = "C# Replace $tring Function";

Response.Write(strText.Replace('$', 'S'));
Output

C# Replace String Function

Above example shows that the C# replace function replaces an old character with new one.

Example 2
string strText = "ab abc abcd abcde abcdef";

Response.Write(strText.Replace("ab", "AB"));
Output

AB ABc ABcd ABcde ABcdef

Above example of C# replace function shows that it replaces the each occurrence of string pattern in the specified string.

Output:

Learn with the help of live sample of C# Replace String Function from the following link:

String Replace Function

Continue to next tutorial: ASP.Net C# IndexOf String Function to learn how to find out the index of the specified string pattern or character from the string text.

3 Responses to "ASP.Net C# Replace String Function"
Thanks a lot. Great work. Your description really helped me lot.
Please Send This Code in my mail id.
shuhaibkc
please send me this code for me
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email