代码之家  ›  专栏  ›  技术社区  ›  this. __curious_geek

使用扩展方法的字符串和日期时间实用程序函数库的建议

  •  4
  • this. __curious_geek  · 技术社区  · 15 年前

    我正在用C语言编写字符串和日期时间实用程序函数的扩展方法库。你能给我建议一些有用的字符串和日期时间的实用函数来帮助我吗?有了你的建议,我可以使它更具凝聚力和集体性。

    谢谢!

    4 回复  |  直到 15 年前
        1
  •  8
  •   Marc Gravell    15 年前
    public static bool IsNullOrEmpty(this string value){
        return string.IsNullOrEmpty(value);
    }
    public static string Reverse(this string value) {
        if (!string.IsNullOrEmpty(value)) {
            char[] chars = value.ToCharArray();
            Array.Reverse(chars);
            value = new string(chars);
        }
        return value;
    }
    public static string ToTitleCase(this string value) {
        return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(value);
    }
    public static string ToTitleCaseInvariant(this string value) {
        return CultureInfo.InvariantCulture.TextInfo.ToTitleCase(value);
    }
    

    微不足道,但打电话更轻率。

        2
  •  5
  •   Joel Coehoorn    15 年前

    那些没有特别说明的方法呢 延伸 字符串或日期时间,而是 目标 或者返回一个字符串或日期时间?然后你可以建造一些 int TimeSpan 方法,因此您可以编写流畅的接口,如:

      DateTime yesterday =  1.Days().Ago();
    

    .

    public static TimeSpan Days(this int value)
    {
        return new TimeSpan(value, 0, 0, 0);
    }
    
    public static TimeSpan Hours(this int value)
    {
        return new TimeSpan(value, 0, 0);
    }
    
    public static TimeSpan Minutes(this int value)
    {
        return new TimeSpan(0, value, 0);
    }
    
    //...
    

    .

    public static DateTime Ago(this TimeSpan value)
    {
        return DateTime.Now.Add(value.Negate());
    }
    
    public static DateTime FromNow(this TimeSpan value)
    {
       return DateTime.Now.Add(value);
    }
    
        3
  •  4
  •   tvanfosson    15 年前

    字符串的扩展

    1. MakeTitle --从标题库字符串生成标题,即将“foo bar”转入“foo bar”。我发现这对打印枚举非常有用: fooEnum.ToString("g").MakeTitle()
    2. Collapse --从两端修剪空白并将所有内部空间折叠到一个单独的空间。
    3. IsNothing --像isNullOrEmpty一样,但首先修剪空白,有助于文本框输入,您不希望只是空格,但如果没有输入,则设置为空。

    日期时间扩展

    1. EndOfDay --将时间设置为给定日期的11:59:59 PM
    2. StartOfDay --将时间设置为给定日期的上午12:00:00
        4
  •  1
  •   Jhonny D. Cano -Leftware-    15 年前

    字符串的扩展

    static string ToCamelCase(this string s) {...}  // Converts a string into Camel Notation, useful for code generation
    static string ToPascalCase(this string s) {...} // Converts a string into Pascal Notation
    static int [Soundex][1](this string s) {...}      // Gets the soundex of a string
    

    日期时间扩展

    static bool IsWithinRange(this DateTime d, DateTime start, DateTime end) {...}
    static string [ToRelativeTime][2](this DateTime d) {...}