i wrote a code for replacing lowercase characters to *. but it does not work. where is the problem?
private void CharacterReplacement()
{
Console.WriteLine("Enter a string to replacement : ");
string TargetString = Console.ReadLine();
string MainString = TargetString;
for (int i = 0; i < TargetString.Length; i++)
{
if (char.IsLower(TargetString[i]))
{
TargetString.Replace(TargetString[i], '*');
}
}
Console.WriteLine("The string {0} has converted to {1}", MainString, TargetString);
}
Replace()
returns a new string, so you need to re-assign it toTargetString
:Another way to express your intend would be with Linq - not sure which I like better, this avoids all the temporary strings but has other overhead:
You can of course write this in one short line by using a regular expression:
Improved version based on Arto's comment, that handles all lowercase unicode characters: