Tuesday 15 February 2011

c# - How to determine if a string is a valid variable name? -


I want a quick way to determine (in C #) that the string is a valid variable name or not. My first experience is to alert some Regx to do this, but I'm thinking that there is a better way to do this. Perhaps a secret method of some kind is hidden somewhere deep, which is called IsThisAValidVariableName (string name), or there is some other clever way to do this that does not suffer from errors that may arise due to the lack of raggex skills.

Try it out:

  // System.CodeDom.Compiler Using; CodeDomProvider Provider = CodeDomProvider.CreateProvider ("C #"); If (provider.IsValidIdentifier (YOUR_VARIABLE_NAME)) {// valid} other {// not valid}  

No comments:

Post a Comment