Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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 The output of the above code is − You can also replace regular characters with asterisks, which is commonly used for masking sensitive information − The output of the above code is − The The output of the above code is − The It replaces all occurrences of the specified character or string The original string remains unchanged ? a new string is returned You can chain multiple 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);
}
}
Initial string = dem* text
After replacing asterisk = demo text
Using Replace() to Replace Characters with Asterisks
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);
}
}
Original: password123
Masked: p***w*rd123
Original email: user@example.com
Masked email: us*r@*x*mpl*.c*m
Using Replace() with Strings
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);
}
}
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
Replace() method is case-sensitive when working with stringsReplace() calls togetherConclusion
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.
