Question

Does anyone know of an algorithm (or external library) that I could call to convert an arbitrary string (i.e. outside my control) to be CLS compliant?

I am generating a dynamic RDLC (Client Report Definition) for an ASP.Net Report Viewer control and some of the field names need to be based on strings entered by the user.

Unfortunately I have little control over the entry of the field names by the client (through a 3rd party CMS). But I am quite flexible around substitutions required to create the compliant string.

I have a reactive hack algorithm for now along the lines of:

public static string FormatForDynamicRdlc(this string s)
{
    //We need to change this string to be CLS compliant.
    return s.Replace(Environment.NewLine, string.Empty)
        .Replace("\t", string.Empty)
        .Replace(",", string.Empty)
        .Replace("-", "_")
        .Replace(" ", "_");
}

But I would love something more comprehensive. Any ideas?

NOTE: If it is of any help, the algorithm I am using to create the dynamic RDLC is based on the BuildRDLC method found here: http://csharpshooter.blogspot.com/2007/08/revised-dynamic-rdlc-generation.html

Was it helpful?

Solution

Here's the algorithm I use to create C/C++ identifiers from arbitrary strings (translated to C#):

    static void Main(string[] args)
    {
        string input = "9\ttotally no @ # way!!!!";
        string safe = string.Concat("_", Regex.Replace(input, "[^a-z0-9_]+", "_"));
        Console.WriteLine(safe);
    }

The leading underscore is unnecessary if the first character of the regex result is not numeric.

OTHER TIPS

Here is a regex which I found could be useful for splitting CLS-compliant string part and non-CLS-compliant string part. Below implementation in C#:

string strRegex = @"^(?<nonCLSCompliantPart>[^A-Za-z]*)(?<CLSCompliantPart>.*)";
Regex myRegex = new Regex(strRegex, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
string strTargetString = @"                            _aaaaaa[5]RoundingHeader";

foreach (Match myMatch in myRegex.Matches(strTargetString))
{
  if (myMatch.Success)
  {
    // Add your code here
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top