INameCreationService.IsValidName(String) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
會取得一個值,表示指定名稱是否有效。
public:
bool IsValidName(System::String ^ name);
public bool IsValidName(string name);
abstract member IsValidName : string -> bool
Public Function IsValidName (name As String) As Boolean
參數
- name
- String
這個名字值得驗證。
傳回
true如果名稱有效;否則,。 false
範例
以下程式碼範例提供了一個方法 INameCreationService.IsValidName 實作範例。 此方法使用字串驗證方案,檢查指定字串的每個字元,以判斷該字串是否為有效名稱。 如果字串有效,方法會回傳 true ,否則 false 。
// Returns whether the specified name contains
// all valid character types.
virtual bool IsValidName( String^ name )
{
for ( int i = 0; i < name->Length; i++ )
{
Char ch = name[ i ];
UnicodeCategory uc = Char::GetUnicodeCategory( ch );
switch ( uc )
{
case UnicodeCategory::UppercaseLetter:
case UnicodeCategory::LowercaseLetter:
case UnicodeCategory::TitlecaseLetter:
case UnicodeCategory::DecimalDigitNumber:
break;
default:
return false;
}
}
return true;
}
// Returns whether the specified name contains
// all valid character types.
public bool IsValidName(string name)
{
for(int i = 0; i < name.Length; i++)
{
char ch = name[i];
UnicodeCategory uc = Char.GetUnicodeCategory(ch);
switch (uc)
{
case UnicodeCategory.UppercaseLetter:
case UnicodeCategory.LowercaseLetter:
case UnicodeCategory.TitlecaseLetter:
case UnicodeCategory.DecimalDigitNumber:
break;
default:
return false;
}
}
return true;
}
' Returns whether the specified name contains
' all valid character types.
Public Function IsValidName(ByVal name As String) As Boolean Implements INameCreationService.IsValidName
Dim i As Integer
For i = 0 To name.Length - 1
Dim ch As Char = name.Chars(i)
Dim uc As UnicodeCategory = [Char].GetUnicodeCategory(ch)
Select Case uc
Case UnicodeCategory.UppercaseLetter, UnicodeCategory.LowercaseLetter, UnicodeCategory.TitlecaseLetter, UnicodeCategory.DecimalDigitNumber
Case Else
Return False
End Select
Next i
Return True
End Function
備註
實作 INameCreationService 中可以有規則來定義有效名稱的參數。 此方法可用於驗證名稱並強制執行這些規則。