如何将任意字符串转换为符合 CLS 的名称?

发布于 2024-09-05 04:59:52 字数 832 浏览 2 评论 0原文

有谁知道我可以调用一个算法(或外部库)来将任意字符串(即在我的控制范围之外)转换为符合 CLS 要求?

我正在为 ASP.Net 报表查看器控件生成动态 RDLC(客户端报表定义),并且某些字段名称需要基于用户输入的字符串。

不幸的是,我几乎无法控制客户端输入的字段名称(通过第 3 方 CMS)。但我对创建兼容字符串所需的替换非常灵活。

我现在有一个反应式黑客算法,大致如下:

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(" ", "_");
}

但我想要更全面的东西。有什么想法吗?

注意:如果有任何帮助,我用来创建动态 RDLC 的算法基于此处找到的 BuildRDLC 方法:http://csharpshooter.blogspot.com/2007/08/revised-dynamic-rdlc- Generation.html

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白日梦 2024-09-12 04:59:53

以下是我用来从任意字符串(翻译为 C#)创建 C/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);
    }

如果正则表达式结果的第一个字符不是数字,则不需要前导下划线。

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.

云淡月浅 2024-09-12 04:59:53

我发现以下正则表达式对于分割符合 CLS 的字符串部分和不符合 CLS 的字符串部分很有用。 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
  }
}

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
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文