【问题标题】:How to extend C# built-in types, like String?如何扩展 C# 内置类型,如 String?
【发布时间】:2011-06-22 01:25:59
【问题描述】:

大家好...我需要 TrimString。但我想删除字符串本身中所有重复的空格,不仅在结尾或开头。我可以用这样的方法来做到这一点:

public static string ConvertWhitespacesToSingleSpaces(string value)
{
    value = Regex.Replace(value, @"\s+", " ");
}

我从here 得到的。但我希望在String.Trim() 本身内调用这段代码,所以我认为我需要扩展或重载或覆盖Trim 方法...有没有办法做到这一点?

提前致谢。

【问题讨论】:

    标签: c# string extension-methods trim


    【解决方案1】:

    因为你不能扩展string.Trim()。您可以按照here 的描述创建一个扩展方法来修剪和减少空格。

    namespace CustomExtensions
    {
        //Extension methods must be defined in a static class
        public static class StringExtension
        {
            // This is the extension method.
            // The first parameter takes the "this" modifier
            // and specifies the type for which the method is defined.
            public static string TrimAndReduce(this string str)
            {
                return ConvertWhitespacesToSingleSpaces(str).Trim();
            }
    
            public static string ConvertWhitespacesToSingleSpaces(this string value)
            {
                return Regex.Replace(value, @"\s+", " ");
            }
        }
    }
    

    你可以这样使用它

    using CustomExtensions;
    
    string text = "  I'm    wearing the   cheese.  It isn't wearing me!   ";
    text = text.TrimAndReduce();
    

    给你

    text = "I'm wearing the cheese. It isn't wearing me!";
    

    【讨论】:

    • 文件是否需要特殊名称?或者你在哪里保存这个?它可以放入 Util 类或类似的东西中吗?
    • @testing 您可以将它们放在任何地方,只要它们被引用到您的项目中。如果您将它们放在特定的命名空间中,只需像任何其他类一样使用“使用”语句将它们带入即可。
    • 为什么不直接从 TrimAndReduce 函数返回正则表达式?它会使您的答案更易于阅读。除非你使用你的答案太多以至于你需要在其他地方调用它 LOL
    【解决方案2】:

    有可能吗?是的,但只能使用扩展方法

    System.String 类是密封的,因此您不能使用覆盖或继承。

    public static class MyStringExtensions
    {
      public static string ConvertWhitespacesToSingleSpaces(this string value)
      {
        return Regex.Replace(value, @"\s+", " ");
      }
    }
    
    // usage: 
    string s = "test   !";
    s = s.ConvertWhitespacesToSingleSpaces();
    

    【讨论】:

    • 明确地说,不,不可能修改String.Trim采取的行动。
    【解决方案3】:

    您的问题分为“是”和“否”。

    是的,您可以使用扩展方法扩展现有类型。扩展方法自然只能访问该类型的公共接口。

    public static string ConvertWhitespacesToSingleSpaces(this string value) {...}
    
    // some time later...
    "hello world".ConvertWhitespacesToSingleSpaces()
    

    不,您不能调用此方法Trim()。扩展方法不参与重载。我认为编译器甚至应该给你一个错误信息,详细说明这一点。

    扩展方法只有在包含定义该方法的类型的命名空间是 using'ed 时才可见。

    【讨论】:

      【解决方案4】:

      Extension methods!

      public static class MyExtensions
      {
          public static string ConvertWhitespacesToSingleSpaces(this string value)
          {
              return Regex.Replace(value, @"\s+", " ");
          }
      }
      

      【讨论】:

        【解决方案5】:

        除了使用扩展方法——这里可能是一个不错的选择——还可以“包装”一个对象(例如“对象组合”)。只要被包装的表单不包含比被​​包装的东西更多的信息,那么被包装的项目可以干净地通过隐式或显式转换而不会丢失信息:只是类型/接口的更改。

        编码愉快。

        【讨论】:

          猜你喜欢
          • 2015-06-28
          • 2014-12-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多