Suggestions for the String and DateTime library of functions using extension methods

I am writing a library of extension functions for String and DateTime utility functions in C #. Could you help me by suggesting useful utlity functions for String and DateTime that you might want to be part of? With your suggestions, I can make it more cohesive and collective.

Thanks!

+3
source share
4 answers
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);
}

Trivial, but a little nicer to call.

+8
source

, DateTime, DateTime? 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);
}
+5

  • MakeTitle - TitleCase,.i.e, "FooBar" "Foo Bar". , Enums: fooEnum.ToString("g").MakeTitle()
  • Collapse - .
  • IsNothing - IsNullOrEmpty, , TextBox, , null, .

DateTime

  • EndOfDay - 11:59:59 PM .
  • StartOfDay - 12:00:00 AM .
+4

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

DateTime

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

Source: https://habr.com/ru/post/1706455/


All Articles