How to replace a character with another character in a string in C#?

String.Replace() Method of String class in C#: Here, we will learn how to replace a character with another character in a string in C#?

Given a string and we have to replace a character with another character in string.

String.Replace()

Method returns modified string after replacing a character with another.

Syntax:

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

Here,
oldChar : is the character to be replaced.
newChar : is the character to replace.

Consider the program:

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!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.