简体   繁体   中英

Java.lang.Character.isUnicodeIdentifierStart() equivalent in .NET

.NET中是否有与Java.lang.Character.isUnicodeIdentifierStart()等效的方法?

There is no method directly for this. In most .NET languages, identifiers can be any Unicode "letter" or an underscore, so you could easily write this as:

bool IsValidIdentifierStart(char ch)
{
    return ch == '_' || char.IsLetter(ch);
}

Note that the CLR is more flexible. The CLS specification, chapter 8.5.1, states:

Assemblies shall follow Annex 7 of Technical Report 15 of the Unicode Standard 3.0 governing the set of characters permitted to start and be included in identifiers, available on-line at http://www.unicode.org/unicode/reports/tr15/tr15-18.html . Identifiers shall be in the canonical format defined by Unicode Normalization Form C.

This allows the runtime to use identifiers that aren't permiitted by many of the standard languages.

You could use Char.IsLetter() as a Unicode Identifier must start with an alphabetical letter...

EDIT: IsLetter() would return true for an underscore, which is invalid, so that should also be checked for.

If you need more than ch == '_' || char.IsLetter(ch) ch == '_' || char.IsLetter(ch) suggested by Reed Copsey than check out Char.GetUnicodeCategory which give you detailed Unicode categorization of a character.

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