简体   繁体   English

C# - 计算字符串长度并将每个字符替换为另一个字符

[英]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 当然,因为你明显散列密码(盐)和存储刚散,你可以不知道实际长度反正;-p

另一种解决方案是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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