简体   繁体   中英

C# - Count string length and replace each character with another

How can I count the number of characters within a string and create another string with the same number of characters but replace all of them with a single character such as "*"? Thank you.

string newString = new string('*', oldString.Length);

Of course, it this is for displaying password equivalents, it might be better to use a fixed number of asterisks - the less clues the better. Of course, since you'd obviously be hashing the password (with salt) and storing just the hash, you couldn't know the actual length anyway ;-p

另一种解决方案是:

Console.Write(System.Text.RegularExpressions.Regex.Replace("test",".", "*"));
var message = "hello world" ;
var newMessage = new String('*', message.Length);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM