views:

78

answers:

1

I need to escape all special characters and replace national characters and get "plain text" for a tablename.

string getTableName(string name)

My string could be "šárka65_%&." and I want to get string I can use in my database as a tablename.

+2  A: 

Which DBMS?

  • In standard SQL, a name enclosed in double quotes is a delimited identifier and may contain any characters.
  • In MS SQL Server, a name enclosed in square brackets is a delimited identifier.
  • In MySQL, a name enclosed in back-ticks is a delimieted identifier.

You could simply choose to enclose the name in the appropriate markers.

I had a feeling that wasn't what you wanted...

What codeset is your string in? It seems to be UTF-8 by the time it gets to my browser. Do you need to be able to invert the mapping unambiguously? That is harder.

You can use many schemes to map the information:

  • One simple minded one is simply to hex-encode everything, using a marker (X) to protect against leading digits:

    XC5A1C3A1726B6136355F25262E
    
  • One slightly less simple minded one is hex-encode anything that is not already an ASCII alphanumeric or underscore.

    XC5A1C3A1rka65_25262E
    
  • Or, as a comment suggests, you can devise a mapping table for accented Latin letters - indeed, a mapping table appropriately initialized will be the fastest approach. The input is the character in the source string; the output is the desired mapped character or characters. If you use an 8-bit character set, this is entirely manageable. If you use full Unicode, it is a lot less manageable (not least, how do you map all the Han syllabary to ASCII?).

  • Or ...

Jonathan Leffler