![](/img/trans.png)
[英]Is there a good way to check if segments overlap in a PathFigure in WPF?
[英]A good way to check a chars 'integrity'
我正在制作自己的密碼生成器; 我希望它創建包含數字、小寫字母和大寫字母的密碼。
我正在使用 Visual Studio 在 C# 中編寫程序。
假設我使用下面的代碼:
Random Rand = new Random();
Char C = Convert.ToChar(Rand.Next(0x30, 0x5A));
如何讓我的程序檢查 (C) 是否等於數字或大寫字母?
使用Char.IsDigit
(用於十進制數字)或Char.IsNumber
和Char.IsUpper
。
這些都是在Char
結構上定義的 static 方法。
一個例子,根據要求:
if(Char.IsNumber(C))
{
// Character is marked as a unicode number
}
您可以檢查是否:
int ordinal = (int)C;
if(( ordinal >= 65 && ordinal <= 90 ) || (ordinal >= 48 && ordinal <= 57))
{
Console.WriteLine( C + " is an uppercase A-Z or 0-9!" );
}
可能只使用 Char.IsNumber 和 Char.IsUpper 方法:參見: http://msdn.microsoft.com/en-us/library/9s91f3by.aspx http://msdn.microsoft.com/en-us/library/yk2b3 .aspx
if (C.IsNumber || C.IsUpper) { // 密碼! }
(不要編寫自己的密碼生成器,除非只是為了好玩)。
更好(和標准)的方法是創建一個允許字符的“字母”字符串,然后在字符串中生成一個隨機索引,select 是一個允許的字符。
比首先生成一個隨機字符然后檢查它是否允許要好得多。
"0123456789abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
請注意刪除的小寫“l”(看起來像 1)。 您應該刪除任何其他內容,例如零與“O”可能...或您的密碼規則允許的任何內容)
以下是使用此技術生成隨機字符串的示例:
public static class RandomUtils
{
private static readonly Random random = new Random();
public static string GenerateRandomAlphaNumericString(int length)
{
const string alpha =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
return GenerateRandomString(length, alpha);
}
public static string GenerateRandomString(int length, string alphabet)
{
int maxlen = alphabet.Length;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++)
{
sb.Append(alphabet[random.Next(0, maxlen)]);
}
return sb.ToString();
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.