×

C# Tutorial

Basics of .Net

C# Basics

C# Fundamentals

C# Operators

C# Loops

C# Type Conversions

C# Exception Handling

C# String Class

C# Arrays

C# List

C# Stack

C# Queue

C# Collections

C# Character Class

C# Class and Object

C# Namespaces

C# Delegates

C# Constructors

C# Inheritance

C# Operator Overloading

C# Structures

C# File Handling

C# Convert.ToInt32()

C# Int32 (int) Struct

C# DateTime Class

C# Uri Class

C# Database Connectivity

C# Windows

C# Other Topics

C# Q & A

C# Programs

C# Find O/P

C# - String.Replace() Method with Example

Learn, how to replace a character with another character in the given string using String.Replace() in C#?
[Last updated : March 21, 2023]

To replace a character with another character in the given string, we use String.Replace() method.

String.Replace()

The String.Replace() method returns modified string after replacing a character with another.

Syntax

String String.Replace(char oldChar, char newChar);

Parameter(s)

  • oldChar : is the character to be replaced.
  • newChar : is the character to replace.

C# program to replace a character with another character in the given string

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1 {
  class Program {
    static void Main() {
      String str1;
      String str2;

      Console.Write("Enter String : ");
      str1 = Console.ReadLine();

      str2 = str1.Replace('i', 'I');

      Console.WriteLine("String after replace method : " + str2);
    }
  }
}

Output

Enter String : This is india
String after replace method : ThIs Is IndIa

Here, we replaced character "i" with the "I".

C# Basic Programs »

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.