views:

323

answers:

4

In Java, there are methods called isJavaIdentifierStart and isJavaIdentifierPart on the Character class that may be used to tell if a string is a valid Java identifier, like so:

public boolean isJavaIdentifier(String s) {
  int n = s.length();
  if (n==0) return false;
  if (!Character.isJavaIdentifierStart(s.charAt(0)))
      return false;
  for (int i = 1; i < n; i++)
      if (!Character.isJavaIdentifierPart(s.charAt(i)))
          return false;
  return true;
}

Is there something like this for C#?

+1  A: 

This can be done using reflection - see http://stackoverflow.com/questions/1829679/how-to-determine-if-a-string-is-a-valid-variable-name

Kragen
+5  A: 

Yes:

// using System.CodeDom.Compiler;
CodeDomProvider provider = CodeDomProvider.CreateProvider("C#");
if (provider.IsValidIdentifier (YOUR_VARIABLE_NAME)) {
      // Valid
} else {
      // Not valid
}

From here: http://stackoverflow.com/questions/1829679/how-to-determine-if-a-string-is-a-valid-variable-name

Mark Byers
This does have some perf. implications you should be aware of. Please see my post for more info.
Scott Wisniewski
+1  A: 

I would be weary of the other solutions offered here. Calling CodeDomProvider.CreateProvider requires finding and parsing the Machine.Config file, as well as your app.config file. That's likely to be several times slower than the time required to just check the string your self.

Instead I would advocate you make one of the following changes:

  1. Cache the provider in a static variable.

    This will cause you to take the hit of creating it only once, but it will slow down type loading.

  2. Create the provider directly, by creating a Microsoft.CSharp.CSharpCodeProvider instance your self

    This will skip the config file parsing all together.

  3. Write the code to implement the check your self.

    If you do this, you get the greatest control over how it's implemented, which can help you optimize performance if you need to. See section 2.2.4 of the C# language spec for the complete lexical grammar for C# identifiers.

Scott Wisniewski
+2  A: 

http://www.morethannothing.co.uk/2008/02/validating-cil-identifiers/ might help.

Basically something like:

const string start = "(\p{Lu}|\p{Ll}|\p{Lt}|\p{Lm}|\p{Lo}|\p{Nl})";
const string extend = "(\p{Mn}|\p{Mc}|\p{Nd}|\p{Pc}|\p{Cf})";
var ident = new Regex(string.Format("{0}({0}|{1})*", start, end);
s = s.Normalize();
ident.Match(s);
ICR