C# String Replace() Method
Last Updated :
11 Mar, 2025
In C#, the Replace() method is used to replace all occurrences of a specified substring or character in a string with another substring or character. This method is particularly useful for string manipulation tasks, such as formatting or cleaning up data.
Example 1: Using Replace() method to replace "World" with "Geeks" in a string.
C#
// C# program to illustrate
// the use of Replace() Method
using System;
class Geeks
{
public static void Main()
{
// String initialization and Declaration
string s = "Hello, World";
// Before Replacing
Console.WriteLine("Before Replacing: " + s);
// Using Replace() method
s = s.Replace("World", "Geeks");
// After Replacing
Console.WriteLine("After Replacing: " + s);
}
}
OutputBefore Replacing: Hello, World
After Replacing: Hello, Geeks
Explanation: In the above example, we use a Replace() method of the String class to replace the World with Geeks.
Syntax of String Replace() Method
public string Replace(char oldChar, char newChar)
public string Replace(string oldValue, string newValue)
Parameters:
- oldChar: The character which is to be replaced.
- newChar: The new character will be replaced with the old character.
- oldValue: The old substring which is to be replaced.
- newValue: New substring that will replace with the old value.
Return Type:
- This method returns a new string (System.String) in which all the occurrences of the previous value change with the specified values.
Exceptions:
- ArgumentNullException: If OldValue or Oldchar both are null.
- ArgumentException If OldValue or Oldchar is the empty string ("").
Example 2: Using the Replace() method to replace all occurrences of a specified substring with a new substring.
C#
// Using Replace(String oldValue, String newValue) method
using System;
class Geeks
{
static void Main(string[] args)
{
string s = "Hello, Geek";
string res = s.Replace("Geek", "Geeks");
Console.WriteLine("" + res);
}
}
Explanation: In the above example, the Replace() method replaces "Geek" with "Geeks" in the original string and returns a new string.
Example 3: Using the Replace() method to replace the specified character from the string.
C#
// Using the Replace() method to replace the
// specified character from the string
using System;
class Geeks
{
static void Main(string[] args)
{
string s = "Hello, World!";
string res = s.Replace('o', 'a');
Console.WriteLine("Modified String: " + res);
}
}
OutputModified String: Hella, Warld!
Explanation: In the above example, we use the Replace() method which replaces all occurrences of a specified character with a new character. As shown the character 'o' replaces with 'a'.
Example 4: Using the Replace() method in a Case-Sensitive Manner.
C#
// Using Replace() method with Case Sensitive
using System;
class Geeks
{
static void Main(string[] args) {
string s = "Hello, World!";
string res = s.Replace("hello", "Geeks");
Console.WriteLine("" + res);
}
}
Explanation: In the above example, the string value passed as "hello" (lowercase) is not found in the original string, so no replacement were done because it is cases case-sensitive operation.
Example 5: Performing multiple replacement operations on the String (Replacement’s Chain) using the Replace() method.
C#
// Perfroming Multiple Replacements
// at the same time
using System;
class Geeks
{
static void Main(string[] args)
{
string s = "Hello, World! Welcome to the World of C#!";
string res = s.Replace("World", "Geeks").Replace("C#", "C Sharp");
Console.WriteLine("Modified String: " + res);
}
}
OutputModified String: Hello, Geeks! Welcome to the Geeks of C Sharp!
Explanation: In the above example, the multiple replacement operation is performed using the Replace() method of string class the world "World" with "Geeks" and "C#" with "C Sharp" using method chaining.
Similar Reads
C# String Remove() Method In C#, the Remove() method of the String class is used for removing the characters from the specified position of a string. If the length is not specified, then it will remove all the characters after the specified position. This method can be overloaded by changing the number of arguments passed to
3 min read
string erase in C++ The string erase() function is a built-in function of the string class that is used to erase the whole or part of the string, shortening its length. The function provides different ways to delete a portion of a string based on either an index position or a range of characters or delete the whole str
4 min read
PHP substr_replace() Function The substr_replace() function is a built-in function in PHP and is used to replace a part of a string with another string. The index in the original string from which the replacement is to be performed needs to be passed as a parameter. If desired, the length up to which the replacement is to be don
3 min read
C# | TrimStart() and TrimEnd() Method Prerequisite: Trim() Method in C# In C#, TrimStart() & TrimEnd() are the string methods. TrimStart() method is used to remove the occurrences of a set of characters specified in an array from the starting of the current String object. TrimEnd() method is used to remove the occurrences of a set o
3 min read
std::string::insert() in C++ In C++, the string insert() function is used to insert characters or a string at the given position of the string. For example,C++#include <bits/stdc++.h> using namespace std; int main() { string s = "Geeks"; // Inserting another string at the friend // of s s.insert(s.size(), "forGeeks"); cou
4 min read
Reverse a String â Complete Tutorial Given a string s, the task is to reverse the string. Reversing a string means rearranging the characters such that the first character becomes the last, the second character becomes second last and so on.Examples:Input: s = "GeeksforGeeks"Output: "skeeGrofskeeG"Explanation : The first character G mo
13 min read
Strings in C A String in C programming is a sequence of characters terminated with a null character '\0'. The C String is work as an array of characters. The difference between a character array and a C string is that the string in C is terminated with a unique character '\0'.DeclarationDeclaring a string in C i
5 min read
Practice questions on Strings String is an important topic from GATE exam point of view. We will discuss key points on strings as well different types of questions based on that. There are two ways to store strings as character array (char p[20]) or a pointer pointing to a string (char* s = âstringâ), both of which can be access
4 min read
std::string::assign() in C++ The member function assign() is used for the assignments, it assigns a new value to the string, replacing its current contents. Syntax 1: Assign the value of string str. string& string::assign (const string& str) str : is the string to be assigned. Returns : *this CPP // CPP code for assign
5 min read
Replace every character of a string by a different character Given a string str of lowercase alphabets only. The task is to replace every character by some new character. The new character belongs to $a-z$ (small case only) Replacement of str[i] is determined by: str[i] = Character obtained after traversing ascii(str[i]) no. of characters in 'a-z' repeatedly
5 min read