C# Program to replace a character with asterisks in a sentence

The Replace() method in C# is used to replace all occurrences of a specified character or string with another character or string. This method is particularly useful when you need to replace special characters like asterisks with other characters in a sentence.

Syntax

Following is the syntax for replacing a character using the Replace() method −

string newString = originalString.Replace(oldChar, newChar);

For replacing a string with another string −

string newString = originalString.Replace(oldString, newString);

Parameters

  • oldChar/oldString: The character or string to be replaced

  • newChar/newString: The character or string to replace with

Return Value

The Replace()

Using Replace() to Replace Asterisks with Characters

Example

using System;

public class Program {
    public static void Main() {
        string str = "dem* text";
        Console.WriteLine("Initial string = " + str);
        
        string result = str.Replace('*', 'o');
        Console.WriteLine("After replacing asterisk = " + result);
    }
}

The output of the above code is −

Initial string = dem* text
After replacing asterisk = demo text

Using Replace() to Replace Characters with Asterisks

You can also replace regular characters with asterisks, which is commonly used for masking sensitive information −

Example

using System;

public class Program {
    public static void Main() {
        string password = "password123";
        Console.WriteLine("Original: " + password);
        
        string masked = password.Replace('a', '*').Replace('s', '*').Replace('o', '*');
        Console.WriteLine("Masked: " + masked);
        
        // Replace all vowels with asterisks
        string email = "user@example.com";
        string maskedEmail = email.Replace('a', '*').Replace('e', '*').Replace('i', '*').Replace('o', '*').Replace('u', '*');
        Console.WriteLine("Original email: " + email);
        Console.WriteLine("Masked email: " + maskedEmail);
    }
}

The output of the above code is −

Original: password123
Masked: p***w*rd123
Original email: user@example.com
Masked email: us*r@*x*mpl*.c*m

Using Replace() with Strings

The Replace() method can also replace entire strings with asterisks or other strings −

Example

using System;

public class Program {
    public static void Main() {
        string sentence = "This is a demo sentence with demo words.";
        Console.WriteLine("Original: " + sentence);
        
        string replaced = sentence.Replace("demo", "****");
        Console.WriteLine("After replacement: " + replaced);
        
        // Case-sensitive replacement
        string text = "Hello World hello world";
        string result = text.Replace("hello", "***");
        Console.WriteLine("Original text: " + text);
        Console.WriteLine("Case-sensitive result: " + result);
    }
}

The output of the above code is −

Original: This is a demo sentence with demo words.
After replacement: This is a **** sentence with **** words.
Original text: Hello World hello world
Case-sensitive result: Hello World *** world

Key Points

  • The Replace() method is case-sensitive when working with strings

  • It replaces all occurrences of the specified character or string

  • The original string remains unchanged ? a new string is returned

  • You can chain multiple Replace() calls together

Conclusion

The Replace() method in C# provides an efficient way to replace characters or strings with asterisks or any other characters. It's commonly used for text processing, data masking, and string manipulation tasks where you need to substitute specific characters or substrings throughout a text.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements